Index: branches/network/src/lib/network/handshake.cc
===================================================================
--- branches/network/src/lib/network/handshake.cc	(revision 6114)
+++ branches/network/src/lib/network/handshake.cc	(revision 6115)
@@ -22,11 +22,22 @@
 #include "handshake.h"
 
-Handshake::Handshake( bool server, int clientId )
+Handshake::Handshake( bool server, int clientId, int entityManagerId )
 {
   /* set the class id for the base object */
   this->setClassID(CL_HANDSHAKE, "Handshake");
 
+  if ( clientId > 255 )
+  {
+    PRINTF(1)("clientId is too big for type byte (%d)! connection to client %d will not work!", clientId, clientId);
+  }
+
+  if ( entityManagerId > 255 )
+  {
+    PRINTF(1)("entityManagerId is too big for type byte (%d)! connection to client %d will not work!", entityManagerId, clientId);
+  }
+
   this->setIsServer(server);
   this->clientId = clientId;
+  this->entityManagerId = entityManagerId;
   this->state = 0;
   this->isOk = false;
@@ -88,5 +99,5 @@
   if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
   {
-    if ( length != 1 )
+    if ( length != 2 )
     {
       PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
@@ -99,4 +110,5 @@
     this->isOk = true;
     this->newHostId = data[0];
+    this->newEntityManagerId = data[1];
 
     if ( newHostId == 0 )
@@ -156,5 +168,5 @@
   if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
   {
-    if ( maxLength < 4 )
+    if ( maxLength < 2 )
     {
       PRINTF(0)("buffer too small for ID");
@@ -177,7 +189,8 @@
       //memcpy(data, &clientId, 4);
       data[0] = (byte)clientId;
+      data[1] = (byte)entityManagerId;
     }
     *reciever = clientId;
-    return 1;
+    return 2;
   }
 
Index: branches/network/src/lib/network/handshake.h
===================================================================
--- branches/network/src/lib/network/handshake.h	(revision 6114)
+++ branches/network/src/lib/network/handshake.h	(revision 6115)
@@ -34,5 +34,5 @@
 {
   public:
-    Handshake(bool server, int clientId = 0);
+    Handshake(bool server, int clientId = 0, int entityManagerId = 0);
     inline bool       completed(){ return hasState( HS_COMPLETED ); }
     inline bool       ok(){ return isOk; }
@@ -49,5 +49,7 @@
     int               state;
     int               clientId;
+    int               entityManagerId;
     int               newHostId;
+    int               newEntityManagerId;
     bool              isOk;
 
Index: branches/network/src/lib/network/network_socket.cc
===================================================================
--- branches/network/src/lib/network/network_socket.cc	(revision 6114)
+++ branches/network/src/lib/network/network_socket.cc	(revision 6115)
@@ -51,6 +51,6 @@
   this->tcpSocket = sock;
 
-  SDL_CreateThread(thread_read, (void*)this);
-  SDL_CreateThread(thread_write, (void*)this);
+  readThread = SDL_CreateThread(thread_read, (void*)this);
+  writeThread = SDL_CreateThread(thread_write, (void*)this);
 }
 
@@ -63,4 +63,7 @@
   incomingBufferLength = 0;
   outgoingBufferLength = 0;
+
+  readThread = NULL;
+  writeThread = NULL;
 
 
@@ -129,6 +132,6 @@
   }
 
-  SDL_CreateThread(thread_read, (void*)this);
-  SDL_CreateThread(thread_write, (void*)this);
+  readThread = SDL_CreateThread(thread_read, (void*)this);
+  writeThread = SDL_CreateThread(thread_write, (void*)this);
 }
 
@@ -310,4 +313,5 @@
   if ( !self->thread_write_running )
   {
+    //delete self;
     SDL_mutexV(self->threadTerminationMutex);
   }
@@ -378,4 +382,5 @@
   if ( !self->thread_read_running )
   {
+    //delete self;
     SDL_mutexV(self->threadTerminationMutex);
   }
Index: branches/network/src/lib/network/network_socket.h
===================================================================
--- branches/network/src/lib/network/network_socket.h	(revision 6114)
+++ branches/network/src/lib/network/network_socket.h	(revision 6115)
@@ -67,4 +67,8 @@
   bool thread_read_running;
   bool thread_write_running;
+
+  SDL_Thread*            readThread;
+  SDL_Thread*            writeThread;
+
 #ifdef _USE_OUTGOING_BUFFER
   static int thread_write(void * data);
