Index: branches/network/src/lib/network/handshake.cc
===================================================================
--- branches/network/src/lib/network/handshake.cc	(revision 6067)
+++ branches/network/src/lib/network/handshake.cc	(revision 6090)
@@ -33,5 +33,5 @@
   this->setOwner(0);
 
-  PRINTF(0)("Handshake created clientId = %d\n", clientId);
+  PRINTF(5)("Handshake created clientId = %d\n", clientId);
 }
 
@@ -88,7 +88,7 @@
   if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
   {
-    if ( length != 4 )
+    if ( length != 1 )
     {
-      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);
+      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
       setState( HS_COMPLETED );
       return;
@@ -98,6 +98,16 @@
     setState( HS_RECVD_HID );
     this->isOk = true;
-    memcpy(&(this->newHostId), data, 4);
-    PRINTF(0)("got my hostID: %d\n", newHostId);
+    this->newHostId = data[0];
+
+    if ( newHostId == 0 )
+    {
+      setState( HS_WAS_REJECT );
+      isOk = false;
+      PRINTF(0)("Server did not accept handshake!\n");
+    }
+    else
+    {
+      PRINTF(0)("got my hostID: %d\n", newHostId);
+    }
     return;
   }
@@ -155,9 +165,19 @@
     setState( HS_SENT_HID );
     setState( HS_COMPLETED );
-    isOk = true;
-    memcpy(data, (byte*)&clientId, 4);
-    if ( this->isServer() )
-      *reciever = clientId;
-    return 4;
+
+    if ( hasState( HS_DO_REJECT ) )
+    {
+      isOk = false;
+      //memcpy(data, (byte*)0, 4);
+      data[0] = 0;
+    }
+    else
+    {
+      isOk = true;
+      //memcpy(data, &clientId, 4);
+      data[0] = (byte)clientId;
+    }
+    *reciever = clientId;
+    return 1;
   }
 
Index: branches/network/src/lib/network/handshake.h
===================================================================
--- branches/network/src/lib/network/handshake.h	(revision 6067)
+++ branches/network/src/lib/network/handshake.h	(revision 6090)
@@ -23,5 +23,8 @@
   HS_SENT_HID   = 0x00000010,
   HS_RECVD_HID  = 0x00000020,
-  HS_COMPLETED  = 0X00000040,
+  HS_COMPLETED  = 0x00000040,
+
+  HS_DO_REJECT  = 0x00010000,
+  HS_WAS_REJECT = 0x00020000,
 
   NUM_STATES
@@ -35,4 +38,6 @@
     inline bool       ok(){ return isOk; }
     inline int        getHostId(){ return newHostId; }
+
+    inline void       doReject(){ setState(HS_DO_REJECT); }
 
     virtual void      writeBytes(const byte* data, int length);
Index: branches/network/src/lib/network/network_protocol.cc
===================================================================
--- branches/network/src/lib/network/network_protocol.cc	(revision 6067)
+++ branches/network/src/lib/network/network_protocol.cc	(revision 6090)
@@ -62,5 +62,5 @@
 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source)
 {
-  PRINTF(0)("create header length = %i, bufferLength = %i\n", length, bufferLength);
+  PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength);
   //If there isn't enough space for the header return -1
   if (length + this->headerLength > bufferLength)
@@ -90,9 +90,9 @@
 Header NetworkProtocol::extractHeader(byte* data, int length)
 {
-  PRINTF(0)("extract Header\n");
+  PRINTF(5)("extract Header\n");
   //Test if received data can contain a header
   if (length < headerLength)
   {
-    PRINTF(0)("Received data is to short; it can't contain a header!\n");
+    PRINTF(1)("Received data is to short; it can't contain a header!\n");
     Header h;
     h.length = 0;
Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 6067)
+++ branches/network/src/lib/network/network_stream.cc	(revision 6090)
@@ -60,4 +60,5 @@
 
   Handshake* hs = new Handshake(false);
+  hs->setUniqueID( 0 );
   this->handshakes.push_back(hs);
   this->connectSynchronizeable(*hs);
@@ -76,4 +77,6 @@
   this->handshakes.push_back( NULL );
   this->bActive = true;
+
+  this->setMaxConnections( 10 );
 }
 
@@ -141,4 +144,20 @@
   if ( this->type == NET_SERVER )
     this->updateConnectionList();
+  else
+  {
+    if ( networkSockets[0] && !networkSockets[0]->isOk() )
+    {
+      PRINTF(1)("lost connection to server\n");
+
+      //delete networkSockets[i];
+      networkSockets[0]->disconnectServer();
+      networkSockets[0]->destroy();
+      networkSockets[0] = NULL;
+      //TODO: delete handshake from synchronizeable list so i can delete it
+      if ( handshakes[0] )
+        delete handshakes[0];
+      handshakes[0] = NULL;
+    }
+  }
 
   for (int i = 0; i<handshakes.size(); i++)
@@ -164,4 +183,6 @@
           PRINT(1)("handshake failed!\n");
           networkSockets[i]->disconnectServer();
+          delete handshakes[i];
+          handshakes[i] = NULL;
           //TODO: handle error
         }
@@ -192,4 +213,9 @@
 
         dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
+
+        //FIXME: this is a hack, find a better way
+        Header* header = (Header*)downBuffer;
+        if ( header->synchronizeableID<100 )
+          header->synchronizeableID = 0;
 
         if ( dataLength<=0 )
@@ -240,5 +266,5 @@
           continue;
 
-        PRINTF(0)("read %d bytes from socket\n", dataLength);
+        PRINTF(5)("read %d bytes from socket\n", dataLength);
         header = networkProtocol->extractHeader(upBuffer, dataLength);
         dataLength -= sizeof(header);
@@ -250,4 +276,9 @@
         }
 
+        if ( header.synchronizeableID == 0 )
+        {
+          header.synchronizeableID = i;
+        }
+
         for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
         {
@@ -259,71 +290,4 @@
     }
   }
-
-#if 0
-  int dataLength = 0;
-
-  /* DOWNSTREAM */
-  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
-  PRINT(0)("============= DOWNSTREAM:===============\n");
-  /* first of all read the synchronizeable's data: */
-  if( this->isServer())
-    dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
-
-  if( dataLength > 0)
-  {
-    /* send the received data to connectionMonitor */
-    this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
-
-    dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
-                 *(this->synchronizeables), 12/* some random number (no real id)*/);
-
-    /* pass the data to the network socket */
- //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
-    /* check if there was an error */
-    if( dataLength == -1)
-    {
-      PRINTF(0)("Error in writing data to the NetworkSocket\n");
-    }
-  }
-
-
-  /* UPSTREAM */
-  dataLength = 0;
-  PRINT(0)("============== UPSTREAM:================\n");
-  /* first of all read the next Orxonox Network Header */
-
-  if( this->state == NET_REC_HEADER)
-  {
-//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
-    if( dataLength == sizeof(Header))
-    {
-      this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
-      printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
-             this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
-             this->packetHeader.receiverID, this->packetHeader.length);
-      /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
-
-      this->state = NET_REC_DATA;
-    }
-  }
-  if( this->state == NET_REC_DATA)
-  {
-    /* now read the data */
-//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
-    /* check if the data is available and process it if so */
-    if( dataLength == this->packetHeader.length)
-    {
-      printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
-      /* send the received data to connectionMonitor */
-      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
-      /* now pass the data to the sync object */
-      if( !this->isServer())
-        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
-
-      this->state = NET_REC_HEADER;
-    }
-  }
-
-#endif
 }
 
@@ -343,10 +307,20 @@
       networkSockets[clientId] = tempNetworkSocket;
       handshakes[clientId] = new Handshake(true, clientId);
+      handshakes[clientId]->setUniqueID(clientId);
     } else
     {
       clientId = networkSockets.size();
       networkSockets.push_back(tempNetworkSocket);
-      handshakes.push_back(new Handshake(true, clientId));
-    }
+      Handshake* tHs = new Handshake(true, clientId);
+      tHs->setUniqueID(clientId);
+      handshakes.push_back(tHs);
+    }
+
+    if ( clientId > this->maxConnections )
+    {
+      handshakes[clientId]->doReject();
+      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
+    }
+    else
 
     PRINTF(0)("New Client: %d\n", clientId);
@@ -388,3 +362,17 @@
 }
 
-
+void NetworkStream::setMaxConnections( int n )
+{
+  if ( !this->isServer() )
+  {
+    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
+  }
+  if ( this->networkSockets.size() > 1 )
+  {
+    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
+    return;
+  }
+  this->maxConnections = n;
+}
+
+
Index: branches/network/src/lib/network/network_stream.h
===================================================================
--- branches/network/src/lib/network/network_stream.h	(revision 6067)
+++ branches/network/src/lib/network/network_stream.h	(revision 6090)
@@ -43,4 +43,7 @@
     inline bool isActive() const { return this->bActive; }
 
+    inline int getMaxConnections(){ return maxConnections; }
+    void setMaxConnections( int n );
+
     virtual void processData();
 
@@ -58,4 +61,5 @@
 
     int                    myHostId;
+    int                    maxConnections;
 
     void updateConnectionList();
Index: branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable.cc	(revision 6067)
+++ branches/network/src/lib/network/synchronizeable.cc	(revision 6090)
@@ -31,4 +31,5 @@
   owner = 0;
   hostID = NetworkManager::getInstance()->getHostID();
+  uniqueID = -1;
   //state = ?;
 
Index: branches/network/src/lib/network/synchronizeable.h
===================================================================
--- branches/network/src/lib/network/synchronizeable.h	(revision 6067)
+++ branches/network/src/lib/network/synchronizeable.h	(revision 6090)
@@ -39,5 +39,5 @@
     bool isServer();
     bool isOutOfSync();
-    void setUniqueId( int id ){ uniqueID = id; }
+    void setUniqueID( int id ){ uniqueID = id; }
     int  getUniqueID() const { return uniqueID; };
     void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
@@ -45,5 +45,5 @@
     inline int getOwner(){ return owner; }
     inline void setOwner(int owner){ this->owner = owner; }
-    
+
     inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
 
@@ -59,5 +59,5 @@
     int state;
     std::list<int> synchronizeRequests;
-    
+
     NetworkStream* networkStream;
 
