Index: branches/network/src/lib/network/handshake.cc
===================================================================
--- branches/network/src/lib/network/handshake.cc	(revision 6043)
+++ branches/network/src/lib/network/handshake.cc	(revision 6053)
@@ -27,18 +27,26 @@
   this->setClassID(CL_HANDSHAKE, "Handshake");
 
-  this->server = server;
+  this->setIsServer(server);
   this->clientId = clientId;
-  this->state = HS_SEND_INIT;
+  this->state = 0;
   this->isOk = false;
+  this->setOwner(0);
+
+  PRINTF(0)("Handshake created clientId = %d\n", clientId);
 }
 
 void Handshake::writeBytes( const byte * data, int length )
 {
-  if ( state == HS_RECV_INIT )
+  PRINTF(0)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state);
+
+  if ( hasState( HS_COMPLETED ) )
+       return;
+
+  if ( !hasState( HS_RECVD_INIT ) )
   {
     if ( length != _INITIAL_DATA_LENGTH )
     {
-      PRINTF(1)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
-      state = HS_COMPLETED;
+      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
+      setState( HS_COMPLETED );
       return;
     }
@@ -46,20 +54,20 @@
     if ( strncmp((char*)data, _INITIAL_DATA, length) )
     {
-      PRINTF(1)("initial packed does not match\n");
-      state = HS_COMPLETED;
+      PRINTF(0)("initial packed does not match\n");
+      setState( HS_COMPLETED );
       return;
     }
 
-    state = HS_SEND_VERSION;
+    setState( HS_RECVD_INIT );
     PRINTF(0)("got valid initial packet from client %d\n", clientId);
     return;
   }
 
-  if ( state == HS_RECV_VERSION )
+  if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
   {
     if ( length != _ORXONOX_VERSION_LENGTH )
     {
-      PRINTF(1)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
-      state = HS_COMPLETED;
+      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
+      setState( HS_COMPLETED );
       return;
     }
@@ -67,13 +75,10 @@
     if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
     {
-      PRINTF(1)("versions do not match\n");
-      state = HS_COMPLETED;
+      PRINTF(0)("versions do not match\n");
+      setState( HS_COMPLETED );
       return;
     }
 
-    if ( this->isServer() )
-      state = HS_SEND_ID;
-    else
-      state = HS_RECV_ID;
+    setState( HS_RECVD_VER );
 
     PRINTF(0)("client %d's version does match\n", clientId);
@@ -81,14 +86,15 @@
   }
 
-  if ( state == HS_RECV_ID )
+  if ( hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
   {
-    if ( length != _ORXONOX_VERSION_LENGTH )
+    if ( length != 4 )
     {
-      PRINTF(1)("hostID packet has wrong size %d instead of %d\n", length, 4);
-      state = HS_COMPLETED;
+      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);
+      setState( HS_COMPLETED );
       return;
     }
 
-    this->state = HS_COMPLETED;
+    setState( HS_COMPLETED );
+    setState( HS_RECVD_HID );
     this->isOk = true;
     memcpy(&(this->newHostId), data, 4);
@@ -99,51 +105,58 @@
 }
 
-int Handshake::readBytes( byte * data, int maxLength, int & reciever )
+int Handshake::readBytes( byte * data, int maxLength, int * reciever )
 {
-  if ( state == HS_SEND_INIT )
+  PRINTF(0)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state);
+
+  if ( hasState( HS_COMPLETED ) )
+    return 0;
+
+  if ( !hasState( HS_SENT_INIT ) )
   {
     if ( maxLength < _INITIAL_DATA_LENGTH )
     {
-      PRINTF(1)("buffer too small for _INITIAL_DATA");
-      state = HS_COMPLETED;
+      PRINTF(0)("buffer too small for _INITIAL_DATA");
+      setState( HS_COMPLETED );
       return 0;
     }
 
-    state = HS_RECV_INIT;
+    setState( HS_SENT_INIT );
     memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
     if ( this->isServer() )
-      reciever = clientId;
+      *reciever = clientId;
     return _INITIAL_DATA_LENGTH;
   }
 
-  if ( state == HS_SEND_VERSION )
+  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
   {
     if ( maxLength < _ORXONOX_VERSION_LENGTH )
     {
-      PRINTF(1)("buffer too small for version number");
-      state = HS_COMPLETED;
+      PRINTF(0)("buffer too small for version number");
+      setState( HS_COMPLETED );
       return 0;
     }
 
-    state = HS_RECV_VERSION;
+    setState( HS_SENT_VER );
     memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
     if ( this->isServer() )
-      reciever = clientId;
+      *reciever = clientId;
     return _ORXONOX_VERSION_LENGTH;
   }
 
-  if ( state == HS_SEND_ID )
+  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
   {
     if ( maxLength < 4 )
     {
-      PRINTF(1)("buffer too small for ID");
-      state = HS_COMPLETED;
+      PRINTF(0)("buffer too small for ID");
+      setState( HS_COMPLETED );
       return 0;
     }
 
-    state = HS_COMPLETED;
+    setState( HS_SENT_HID );
+    setState( HS_COMPLETED );
+    isOk = true;
     memcpy(data, (byte*)&clientId, 4);
     if ( this->isServer() )
-      reciever = clientId;
+      *reciever = clientId;
     return 4;
   }
Index: branches/network/src/lib/network/handshake.h
===================================================================
--- branches/network/src/lib/network/handshake.h	(revision 6043)
+++ branches/network/src/lib/network/handshake.h	(revision 6053)
@@ -17,11 +17,11 @@
 
 typedef enum HandshakeState {
-  HS_SEND_INIT = 0,
-  HS_RECV_INIT,
-  HS_SEND_VERSION,
-  HS_RECV_VERSION,
-  HS_SEND_ID,
-  HS_RECV_ID,
-  HS_COMPLETED,
+  HS_SENT_INIT  = 0x00000001,
+  HS_RECVD_INIT = 0x00000002,
+  HS_SENT_VER   = 0x00000004,
+  HS_RECVD_VER  = 0x00000008,
+  HS_SENT_HID   = 0x00000010,
+  HS_RECVD_HID  = 0x00000020,
+  HS_COMPLETED  = 0X00000040,
 
   NUM_STATES
@@ -32,10 +32,10 @@
   public:
     Handshake(bool server, int clientId = 0);
-    inline bool       completed(){ return state == HS_COMPLETED; }
+    inline bool       completed(){ return hasState( HS_COMPLETED ); }
     inline bool       ok(){ return isOk; }
     inline int        getHostId(){ return newHostId; }
 
     virtual void      writeBytes(const byte* data, int length);
-    virtual int       readBytes(byte* data, int maxLength, int& reciever);
+    virtual int       readBytes(byte* data, int maxLength, int * reciever);
     virtual void      writeDebug() const;
     virtual void      readDebug() const;
@@ -43,8 +43,11 @@
   private:
     int               state;
-    int               server;
     int               clientId;
     int               newHostId;
     bool              isOk;
+
+    inline bool       hasState( int a ){ return (state & a) == a; }
+    inline void       setState( int a ){ state = state | a; }
+    inline void       unsetState( int a ){ state = state & (~a); }
 
 };
Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 6043)
+++ branches/network/src/lib/network/network_stream.cc	(revision 6053)
@@ -29,4 +29,5 @@
 #include "list.h"
 #include "debug.h"
+#include "class_list.h"
 
 /* include your own header */
@@ -73,4 +74,5 @@
   this->connectionMonitor = new ConnectionMonitor();
   this->networkSockets.push_back( NULL );
+  this->handshakes.push_back( NULL );
   this->bActive = true;
 }
@@ -128,20 +130,21 @@
 void NetworkStream::processData()
 {
-  //PRINTF(0)("processData()\n");
+  PRINTF(0)("processData()\n");
   if ( this->type == NET_SERVER )
     this->updateConnectionList();
 
-  for (HandshakeVector::iterator it = handshakes.begin(); it!=handshakes.end(); it++)
-  {
-    if ( *it )
-    {
-      if ( (*it)->completed() )
+  for (int i = 0; i<handshakes.size(); i++)
+  {
+    if ( handshakes[i] )
+    {
+      if ( handshakes[i]->completed() )
       {
-        if ( (*it)->ok() )
-        {
-          NetworkManager::getInstance()->setHostID( (*it)->getHostId() );
-          myHostId = NetworkManager::getInstance()->getHostID();
-          delete *it;
-          *it = NULL;
+        if ( handshakes[i]->ok() )
+        {
+          if ( type != NET_SERVER )
+          {
+            NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
+            myHostId = NetworkManager::getInstance()->getHostID();
+          }
           PRINT(0)("handshake finished\n");
         }
@@ -149,6 +152,5 @@
         {
           PRINT(1)("handshake failed!\n");
-          delete *it;
-          *it = NULL;
+          networkSockets[i]->disconnectServer();
           //TODO: handle error
         }
@@ -165,10 +167,11 @@
   for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
   {
-    if ( (*it)->getOwner() == myHostId )
-    {
-      PRINTF(0)("sync: %s\n", (*it)->getName());
-      reciever = 0;
+    if ( (*it) && (*it)->getOwner() == myHostId )
+    {
       do {
-        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, reciever);
+        reciever = 0;
+        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
+
+        PRINTF(0)("reciever = %d\n", reciever);
 
         if ( dataLength<=0 ){
@@ -179,5 +182,15 @@
         PRINTF(0)("read %d bytes\n", dataLength);
 
-        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
+        if (ClassList::exists((*it)))
+        {
+          PRINTF(0)("before cast\n");
+          dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
+          PRINTF(0)("after cast\n");
+        }
+        else
+        {
+          PRINTF(0)("instance does not exist anymore\n");
+          //synchronizeables.remove(it);
+        }
 
         if ( dataLength<=0 )
@@ -188,5 +201,5 @@
           if ( networkSockets[reciever] )
           {
-            PRINTF(0)("write %d bytes to socket\n", dataLength);
+            PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
             networkSockets[reciever]->writePacket(downBuffer, dataLength);
           }
@@ -198,8 +211,11 @@
         else
         {
-          for ( int i = 1; i<networkSockets.size(); i++)
+          for ( int i = 0; i<networkSockets.size(); i++)
           {
             if ( networkSockets[i] )
+            {
+              PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
               networkSockets[i]->writePacket(downBuffer, dataLength);
+            }
           }
         }
@@ -211,5 +227,5 @@
   /* UPSTREAM */
 
-  for ( int i = 1; i<networkSockets.size(); i++)
+  for ( int i = 0; i<networkSockets.size(); i++)
   {
     if ( networkSockets[i] )
Index: branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable.cc	(revision 6043)
+++ branches/network/src/lib/network/synchronizeable.cc	(revision 6053)
@@ -14,4 +14,6 @@
    co-programmer: Benjamin Wuest
 */
+
+#define DEBUG_MODULE_NETWORK
 
 #include "synchronizeable.h"
@@ -51,11 +53,15 @@
  */
 void Synchronizeable::writeBytes(const byte* data, int length)
-{}
+{
+  PRINTF(1)("Synchronizeable::writeBytes was called\n");
+}
 
 /**
  *  read data from NetworkStream
  */
-int Synchronizeable::readBytes(byte* data, int maxLength, int& reciever)
-{}
+int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever)
+{
+  PRINTF(1)("Synchronizeable::readBytes was called\n");
+}
 
 
Index: branches/network/src/lib/network/synchronizeable.h
===================================================================
--- branches/network/src/lib/network/synchronizeable.h	(revision 6043)
+++ branches/network/src/lib/network/synchronizeable.h	(revision 6053)
@@ -29,5 +29,5 @@
 
     virtual void      writeBytes(const byte* data, int length);
-    virtual int       readBytes(byte* data, int maxLength, int& reciever);
+    virtual int       readBytes(byte* data, int maxLength, int * reciever);
     virtual void      writeDebug() const;
     virtual void      readDebug() const;
