Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 6062)
+++ 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;
+}
+
+
