Index: code/branches/network/src/network/ConnectionManager.cc
===================================================================
--- code/branches/network/src/network/ConnectionManager.cc	(revision 1409)
+++ code/branches/network/src/network/ConnectionManager.cc	(revision 1491)
@@ -65,4 +65,5 @@
   
   ConnectionManager::ConnectionManager():receiverThread_(0){}
+  boost::recursive_mutex ConnectionManager::enet_mutex_;
   
   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
@@ -143,4 +144,5 @@
     if(!temp)
       return false;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0)
       return false;
@@ -152,4 +154,5 @@
     if(!temp)
       return false;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0)
       return false;
@@ -158,4 +161,5 @@
 
   bool ConnectionManager::addPacketAll(ENetPacket *packet) {
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
       if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
@@ -177,4 +181,5 @@
     if(server==NULL)
       return false;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     enet_host_flush(server);
     return true;
@@ -184,7 +189,10 @@
     // what about some error-handling here ?
     ENetEvent *event;
-    enet_initialize();
     atexit(enet_deinitialize);
-    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
+    { //scope of the mutex
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+      enet_initialize();
+      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
+    }
     if(server==NULL){
       // add some error handling here ==========================
@@ -195,8 +203,12 @@
     while(!quit){
       event = new ENetEvent;
-      if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
-        // we should never reach this point
-        quit=true;
-        // add some error handling here ========================
+      { //mutex scope
+        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
+          // we should never reach this point
+          quit=true;
+          continue;
+          // add some error handling here ========================
+        }
       }
       switch(event->type){
@@ -231,5 +243,8 @@
     disconnectClients();
     // if we're finishied, destroy server
-    enet_host_destroy(server);
+    {
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+      enet_host_destroy(server);
+    }
   }
   
@@ -241,11 +256,14 @@
     ClientInformation *temp = head_->next();
     while(temp!=0){
-      enet_peer_disconnect(temp->getPeer(), 0);
+      {
+        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+        enet_peer_disconnect(temp->getPeer(), 0);
+      }
       temp = temp->next();
     }
     //bugfix: might be the reason why server crashes when clients disconnects
-    //temp = temp->next();
     temp = head_->next();
-    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
       switch (event.type)
       {
@@ -273,8 +291,4 @@
   }
 
-  /*bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
-    COUT(4) << "removing client from list" << std::endl;
-    return removeClient(head_->findClient(&(peer->address))->getID());
-  }*/
 /**
 This function adds a client that connects to the clientlist of the server
@@ -422,5 +436,8 @@
   
   void ConnectionManager::disconnectClient(ClientInformation *client){
-    enet_peer_disconnect(client->getPeer(), 0);
+    {
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+      enet_peer_disconnect(client->getPeer(), 0);
+    }
     removeShip(client);
   }
