Index: code/trunk/src/network/CMakeLists.txt
===================================================================
--- code/trunk/src/network/CMakeLists.txt	(revision 1360)
+++ code/trunk/src/network/CMakeLists.txt	(revision 1502)
Index: code/trunk/src/network/Client.cc
===================================================================
--- code/trunk/src/network/Client.cc	(revision 1360)
+++ code/trunk/src/network/Client.cc	(revision 1502)
@@ -85,4 +85,5 @@
     isConnected=false;
     isSynched_=false;
+    gameStateFailure_=false;
   }
 
@@ -95,4 +96,5 @@
     isConnected=false;
     isSynched_=false;
+    gameStateFailure_=false;
   }
 
@@ -105,4 +107,5 @@
     isConnected=false;
     isSynched_=false;
+    gameStateFailure_=false;
   }
 
@@ -120,9 +123,9 @@
     isConnected=client_connection.createConnection();
     if(isConnected){
-      COUT(3) << "sending connectrequest" << std::endl;
-      if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
-        COUT(1) << "could not create connection" << std::endl;
+//       COUT(3) << "sending connectrequest" << std::endl;
+//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
+//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
     }else
-      COUT(1) << "could not create connection" << std::endl;
+      COUT(1) << "could not create connection laber" << std::endl;
     return isConnected;
   }
@@ -137,34 +140,7 @@
   }
 
-  /**
-  * submits a MouseAction to the server
-  * @param x x Coordinate
-  * @param y y Coordinate
-  * @return true/false
-  */
-  bool Client::sendMouse(double x, double y){
-    // generate packet and add it to the queue
-    if(!isConnected)
-      return false;
-    if(!client_connection.addPacket(pck_gen.mousem(x, y)))
-      return false;
-    // send packets
-    return client_connection.sendPackets();
-  }
-
-  /**
-  * submits a Keystrike to the server
-  * @param key_code code to submit
-  * @return true/false
-  */
-  bool Client::sendKeyboard(char key_code){
-    // generate packet and add it to queue
-    if(!isConnected)
-      return false;
-    if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
-      return false;
-    // send packets
-    return client_connection.sendPackets();
-  }
+  
+
+  
 
   /**
@@ -177,50 +153,8 @@
     if(!isConnected)
       return false;
-    if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))
-      return client_connection.sendPackets();
+    return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
+      //return client_connection.sendPackets();
     // send packets
     return false;
-  }
-
-  /**
-  * Adds a MouseAction to the PacketQueue
-  * @param x x Coordinate
-  * @param y y Coordinate
-  * @return true/false
-  */
-  bool Client::addMouse(double x, double y){
-    // generate packet and add it to the queue
-    if(client_connection.addPacket(pck_gen.mousem(x, y)))
-      return true;
-    else
-      return false;
-  }
-
-  /**
-  * Adds a Keystrike to the PacketQueue
-  * @param key_code
-  * @return true/false
-  */
-  bool Client::addKeyboard(char key_code){
-    // generate packet and add it to queue
-    if(client_connection.addPacket(pck_gen.keystrike(key_code)))
-      return true;
-    else
-      return false;
-  }
-
-  /**
-  * Sends out all the packets queued by addXXX
-  */
-  bool Client::sendPackets(){
-    if(!isConnected)
-      return false;
-    ENetEvent event;
-    // send packets
-    client_connection.sendPackets(&event);
-    if(event.type==ENET_EVENT_TYPE_NONE)
-      return true;
-    else
-      return false;
   }
 
@@ -243,28 +177,38 @@
       }
     }
-    ENetPacket *packet;
+    ENetEvent *event;
     // stop if the packet queue is empty
     while(!(client_connection.queueEmpty())){
-      packet = client_connection.getPacket();
-      COUT(5) << "tick packet size " << packet->dataLength << std::endl;
-      elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
-    }
-    if(!client_connection.sendPackets())
-      COUT(3) << "Problem sending packets to server" << std::endl;
-    return;
-  }
-
-  void Client::processGamestate( GameStateCompressed *data, int clientID){
-    int id = data->id;
-    COUT(5) << "received gamestate id: " << data->id << std::endl;
-    if(gamestate.pushGameState(data)){
+      event = client_connection.getEvent();
+      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
+      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
+    }
+    int gameStateID = gamestate.processGameState();
+    if(gameStateID==GAMESTATEID_INITIAL)
+      if(gameStateFailure_){
+        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
+          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
+        else 
+          COUT(4) << "negatively acked a gamestate" << std::endl;
+        }
+      else
+        gameStateFailure_=true;
+    else if(gameStateID!=0){
+      // ack gamestate and set synched
       if(!isSynched_)
         isSynched_=true;
-      if(!client_connection.addPacket(pck_gen.acknowledgement(id)))
-        return;
-        // we do this at the end of a tick
-      if(!client_connection.sendPackets())
-        COUT(2) << "Could not send acknowledgment" << std::endl;
-    }
+      gameStateFailure_=false;
+      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
+        COUT(3) << "could not ack gamestate" << std::endl;
+    }// otherwise we had no gamestate to load
+    gamestate.cleanup();
+    /*if(!client_connection.sendPackets())
+      COUT(3) << "Problem sending packets to server" << std::endl;*/
+    return;
+  }
+
+  void Client::processGamestate( GameStateCompressed *data, int clientID){
+    COUT(5) << "received gamestate id: " << data->id << std::endl;
+    gamestate.addGameState(data);
   }
 
@@ -275,4 +219,5 @@
       id->setNetworkID(clid->clid);
     COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
+    COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
     delete clid;
     return;
Index: code/trunk/src/network/Client.h
===================================================================
--- code/trunk/src/network/Client.h	(revision 1360)
+++ code/trunk/src/network/Client.h	(revision 1502)
@@ -75,12 +75,5 @@
     bool closeConnection();
 
-    bool sendMouse(double x, double y);
-    bool sendKeyboard(char key_code);
     bool sendChat( std::string message );
-
-    bool addMouse(double x, double y);
-    bool addKeyboard(char key_code);
-
-    bool sendPackets();
     
     int getShipID(){return shipID_;}
@@ -110,4 +103,5 @@
     int clientID_;     // this is the id the server gave to us
     int shipID_;
+    bool gameStateFailure_;
   };
 
Index: code/trunk/src/network/ClientConnection.cc
===================================================================
--- code/trunk/src/network/ClientConnection.cc	(revision 1360)
+++ code/trunk/src/network/ClientConnection.cc	(revision 1502)
@@ -50,4 +50,6 @@
 {
   //static boost::thread_group network_threads;
+
+  boost::recursive_mutex ClientConnection::enet_mutex_;
 
   ClientConnection::ClientConnection(int port, std::string address) {
@@ -75,5 +77,5 @@
 
 
-  ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
+  /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
     if(!buffer.isEmpty()) {
       //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
@@ -88,4 +90,11 @@
     ENetAddress address; //sems that address is not needed
     return getPacket(address);
+  }*/
+  
+  ENetEvent *ClientConnection::getEvent(){
+    if(!buffer.isEmpty())
+      return buffer.pop();
+    else
+      return NULL;
   }
 
@@ -98,5 +107,5 @@
     //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
     // wait 10 seconds for the connection to be established
-    return waitEstablished(10000);
+    return waitEstablished(3000);
   }
 
@@ -117,4 +126,5 @@
       return false;
     }
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     if(enet_peer_send(server, 0, packet)<0)
       return false;
@@ -123,52 +133,57 @@
   }
 
-  bool ClientConnection::sendPackets(ENetEvent *event) {
+  bool ClientConnection::sendPackets() {
     if(server==NULL)
       return false;
-    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
-      return true;}
-    else
-      return false;
-  }
-
-  bool ClientConnection::sendPackets() {
-    ENetEvent event;
-    if(server==NULL)
-      return false;
-    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
-      return true;}
-    else
-      return false;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+    enet_host_flush(client);
+    lock.unlock();
+    return true;
   }
 
   void ClientConnection::receiverThread() {
     // what about some error-handling here ?
-    enet_initialize();
     atexit(enet_deinitialize);
-    ENetEvent event;
-    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
-    if(client==NULL)
+    ENetEvent *event;
+    {
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+      enet_initialize();
+      client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
+      lock.unlock();
+    }
+    if(client==NULL) {
+      COUT(2) << "ClientConnection: could not create client host" << std::endl;
       // add some error handling here ==========================
       quit=true;
+    }
     //connect to the server
     if(!establishConnection()){
+      COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl;
       quit=true;
       return;
     }
+    event = new ENetEvent;
     //main loop
     while(!quit){
       //std::cout << "connection loop" << std::endl;
-      if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
-        // we should never reach this point
-        quit=true;
-        // add some error handling here ========================
+      {
+        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+        if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
+          // we should never reach this point
+          quit=true;
+          continue;
+          // add some error handling here ========================
+        }
+        lock.unlock();
       }
-      switch(event.type){
+      switch(event->type){
         // log handling ================
       case ENET_EVENT_TYPE_CONNECT:
+        break;
       case ENET_EVENT_TYPE_RECEIVE:
         COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
-        if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
+        if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
         COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
+        event = new ENetEvent;
         break;
       case ENET_EVENT_TYPE_DISCONNECT:
@@ -178,7 +193,8 @@
         break;
       case ENET_EVENT_TYPE_NONE:
-        continue;
+        //receiverThread_->yield();
+        usleep(1000);
+        break;
       }
-      //receiverThread_->yield();
     }
     // now disconnect
@@ -186,4 +202,5 @@
     if(!disconnectConnection())
       // if disconnecting failed destroy conn.
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
       enet_peer_reset(server);
     return;
@@ -192,4 +209,5 @@
   bool ClientConnection::disconnectConnection() {
     ENetEvent event;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     enet_peer_disconnect(server, 0);
     while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
@@ -212,15 +230,20 @@
     ENetEvent event;
     // connect to peer (server is type ENetPeer*)
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
-    if(server==NULL)
+    if(server==NULL) {
+      COUT(2) << "ClientConnection: server == NULL" << std::endl;
       // error handling
       return false;
+    }
     // handshake
-    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
+    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){
       established=true;
       return true;
     }
-    else
-      return false;
+    else {
+      COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
+      return false;
+    }
   }
 
Index: code/trunk/src/network/ClientConnection.h
===================================================================
--- code/trunk/src/network/ClientConnection.h	(revision 1360)
+++ code/trunk/src/network/ClientConnection.h	(revision 1502)
@@ -54,6 +54,5 @@
 #define NETWORK_PORT 55556
 #define NETWORK_CLIENT_MAX_CONNECTIONS 5
-#define NETWORK_CLIENT_TIMEOUT 100
-#define NETWORK_SEND_WAIT 5
+#define NETWORK_CLIENT_TIMEOUT 1
 #define NETWORK_CLIENT_CHANNELS 2
 
@@ -63,6 +62,7 @@
     ClientConnection(int port, std::string address);
     ClientConnection(int port, const char* address);
-    ENetPacket *getPacket(ENetAddress &address); // thread1
-    ENetPacket *getPacket(); // thread1
+    //ENetPacket *getPacket(ENetAddress &address); // thread1
+    //ENetPacket *getPacket(); // thread1
+    ENetEvent *getEvent();
     // check wheter the packet queue is empty
     bool queueEmpty();
@@ -75,5 +75,5 @@
     bool sendPackets();
     // send out all queued packets and save result in event
-    bool sendPackets(ENetEvent *event);
+    //bool sendPackets(ENetEvent *event);
     bool waitEstablished(int milisec);
     bool isConnected(){return established;}
@@ -95,4 +95,6 @@
     ENetPeer *server;
     boost::thread *receiverThread_;
+    
+    static boost::recursive_mutex enet_mutex_;
   };
 
Index: code/trunk/src/network/ClientInformation.cc
===================================================================
--- code/trunk/src/network/ClientInformation.cc	(revision 1360)
+++ code/trunk/src/network/ClientInformation.cc	(revision 1502)
@@ -45,5 +45,4 @@
 namespace network
 {
-  boost::recursive_mutex ClientInformation::mutex_;
   
   ClientInformation::ClientInformation() {
@@ -84,13 +83,11 @@
 
   ClientInformation::~ClientInformation() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
-    if(preve!=0)
-      preve->setNext(this->nexte);
-    if(nexte!=0)
-      nexte->setPrev(this->preve);
+    if(prev()!=0)
+      prev()->setNext(this->next());
+    if(next()!=0)
+      next()->setPrev(this->prev());
   }
 
   ClientInformation *ClientInformation::next() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this!=0)
       return this->nexte;
@@ -99,5 +96,4 @@
   }
   ClientInformation *ClientInformation::prev() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this!=0)
       return this->preve;
@@ -107,5 +103,4 @@
 
   bool ClientInformation::setPrev(ClientInformation *prev) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!head_)
       this->preve = prev;
@@ -116,5 +111,4 @@
 
   bool ClientInformation::setNext(ClientInformation *next) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     this->nexte = next;
     return true;
@@ -122,7 +116,6 @@
 
   ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
-    this->nexte->setPrev(ins);
-    ins->setNext(this->nexte);
+    this->next()->setPrev(ins);
+    ins->setNext(this->next());
     ins->setPrev(this);
     this->nexte = ins;
@@ -131,9 +124,8 @@
 
   ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return NULL;
     this->prev()->setNext(ins);
-    ins->setPrev(this->preve);
+    ins->setPrev(this->prev());
     this->preve=ins;
     ins->setNext(this);
@@ -142,10 +134,10 @@
 
   void ClientInformation::setID(int clientID){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
+    if(!this)
+      return;
     clientID_ = clientID;
   }
 
   bool ClientInformation::setPeer(ENetPeer *peer){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return false;
@@ -154,6 +146,5 @@
   }
 
-  bool ClientInformation::setGamestateID(int id){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
+  bool ClientInformation::setGameStateID(int id){
     if(!this)
       return false;
@@ -163,5 +154,4 @@
   
   bool ClientInformation::setPartialGamestateID(int id){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return false;
@@ -171,5 +161,4 @@
 
   int ClientInformation::getID() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return CLIENTID_UNKNOWN;
@@ -179,5 +168,4 @@
 
   ENetPeer *ClientInformation::getPeer() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this)
       return peer_;
@@ -187,28 +175,22 @@
   
   bool ClientInformation::getHead(){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     return head_;
   }
   
   void ClientInformation::setHead(bool h){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     head_=h;
   }
   
   int ClientInformation::getFailures(){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     return failures_;
   }
   void ClientInformation::addFailure(){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     failures_++;
   }
   void ClientInformation::resetFailures(){
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     failures_=0;
   }
 
   int ClientInformation::getGamestateID() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this)
       return gamestateID_;
@@ -218,5 +200,4 @@
   
   int ClientInformation::getPartialGamestateID() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this)
       return partialGamestateID_;
@@ -226,5 +207,4 @@
 
   ClientInformation *ClientInformation::insertBack(ClientInformation *ins) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return NULL;
@@ -239,5 +219,4 @@
 
   bool ClientInformation::removeClient(int clientID) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this || clientID==CLIENTID_UNKNOWN)
       return false;
@@ -252,5 +231,4 @@
 
   bool ClientInformation::removeClient(ENetPeer *peer) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this || !peer)
       return false;
@@ -275,5 +253,4 @@
   */
   ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     ClientInformation *temp = this;
     if (temp->head_)
@@ -293,5 +270,4 @@
   */
   ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     ClientInformation *temp = this;
     while(temp!=0){
@@ -309,5 +285,4 @@
 
   bool ClientInformation::setSynched(bool s) {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(!this)
       return false;
@@ -317,5 +292,4 @@
 
   bool ClientInformation::getSynched() {
-    boost::recursive_mutex::scoped_lock lock(mutex_);
     if(this)
       return synched_;
Index: code/trunk/src/network/ClientInformation.h
===================================================================
--- code/trunk/src/network/ClientInformation.h	(revision 1360)
+++ code/trunk/src/network/ClientInformation.h	(revision 1502)
@@ -49,4 +49,6 @@
 #define CLIENTID_UNKNOWN -2
 
+// WATCH OUT: THE CLIENTINFORMATION LIST IS NOT THREADSAFE ANYMORE
+
 namespace network
 {
@@ -69,5 +71,5 @@
     void setID(int clientID);
     bool setPeer(ENetPeer *peer);
-    bool setGamestateID(int id);
+    bool setGameStateID(int id);
     bool setPartialGamestateID(int id);
     inline void setShipID(int id){ShipID_=id;}
@@ -114,5 +116,4 @@
     bool head_;
     unsigned short failures_;
-    static boost::recursive_mutex mutex_;
     
   };
Index: code/trunk/src/network/ConnectionManager.cc
===================================================================
--- code/trunk/src/network/ConnectionManager.cc	(revision 1360)
+++ code/trunk/src/network/ConnectionManager.cc	(revision 1502)
@@ -44,6 +44,7 @@
 #include "core/CoreIncludes.h"
 #include "core/BaseObject.h"
+#include "objects/SpaceShip.h"
 #include "util/Math.h"
-#include "objects/SpaceShip.h"
+#include "util/Sleep.h"
 #include "ClientInformation.h"
 #include "ConnectionManager.h"
@@ -65,4 +66,5 @@
   
   ConnectionManager::ConnectionManager():receiverThread_(0){}
+  boost::recursive_mutex ConnectionManager::enet_mutex_;
   
   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
@@ -72,4 +74,11 @@
     head_ = head;
   }
+  
+  ConnectionManager::ConnectionManager(ClientInformation *head, int port){
+    quit=false;
+    bindAddress.host = ENET_HOST_ANY;
+    bindAddress.port = port;
+    head_ = head;
+  }
 
   ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) {
@@ -87,15 +96,15 @@
   }
 
-  ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
+  /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
     if(!buffer.isEmpty())
       return buffer.pop(address);
     else
       return NULL;
-  }
+  }*/
 /**
 This function only pops the first element in PacketBuffer (first in first out)
 used by processQueue in Server.cc
 */
-  ENetPacket *ConnectionManager::getPacket(int &clientID) {
+  /*ENetPacket *ConnectionManager::getPacket(int &clientID) {
     ENetAddress address;
     ENetPacket *packet=getPacket(address);
@@ -105,4 +114,11 @@
     clientID=temp->getID();
     return packet;
+  }*/
+  
+  ENetEvent *ConnectionManager::getEvent(){
+    if(!buffer.isEmpty())
+      return buffer.pop();
+    else
+      return NULL;
   }
 
@@ -129,4 +145,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;
@@ -136,12 +153,18 @@
   bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
     ClientInformation *temp = head_->findClient(clientID);
-    if(!temp)
-      return false;
-    if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0)
-      return false;
+    if(!temp){
+      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
+      return false;
+    }
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+    if(enet_peer_send(temp->getPeer(), 0, packet)!=0){
+      COUT(3) << "C.Man: addPacket enet_peer_send failed" << std::endl;
+      return false;
+    }
     return true;
   }
 
   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)
@@ -151,29 +174,24 @@
   }
 
-  bool ConnectionManager::sendPackets(ENetEvent *event) {
+  // we actually dont need that function, because host_service does that for us
+  bool ConnectionManager::sendPackets() {
     if(server==NULL)
       return false;
-    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
-      return true;
-    else
-      return false;
-  }
-
-  bool ConnectionManager::sendPackets() {
-    ENetEvent event;
-    if(server==NULL)
-      return false;
-    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
-      return true;
-    else
-      return false;
+    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+    enet_host_flush(server);
+    lock.unlock();
+    return true;
   }
 
   void ConnectionManager::receiverThread() {
     // what about some error-handling here ?
-    enet_initialize();
+    ENetEvent *event;
     atexit(enet_deinitialize);
-    ENetEvent *event = new ENetEvent;
-    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);
+      lock.unlock();
+    }
     if(server==NULL){
       // add some error handling here ==========================
@@ -182,39 +200,53 @@
     }
 
+    event = new ENetEvent;
     while(!quit){
-      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 ========================
+        }
+        lock.unlock();
       }
       switch(event->type){
         // log handling ================
         case ENET_EVENT_TYPE_CONNECT:
-          addClient(event);
+          COUT(3) << "adding event_type_connect to queue" << std::endl;
+        case ENET_EVENT_TYPE_DISCONNECT:
+          //addClient(event);
           //this is a workaround to ensure thread safety
-          COUT(5) << "Con.Man: connection event has occured" << std::endl;
-          break;
+          //COUT(5) << "Con.Man: connection event has occured" << std::endl;
+          //break;
         case ENET_EVENT_TYPE_RECEIVE:
           //std::cout << "received data" << std::endl;
           COUT(5) << "Con.Man: receive event has occured" << std::endl;
           // only add, if client has connected yet and not been disconnected
-          if(head_->findClient(&event->peer->address))
+          //if(head_->findClient(&event->peer->address))
             processData(event);
-          else
-            COUT(3) << "received a packet from a client we don't know" << std::endl;
+            event = new ENetEvent;
+//           else
+//             COUT(3) << "received a packet from a client we don't know" << std::endl;
           break;
-        case ENET_EVENT_TYPE_DISCONNECT:
-          clientDisconnect(event->peer);
+        //case ENET_EVENT_TYPE_DISCONNECT:
+          //clientDisconnect(event->peer);
+          //break;
+        case ENET_EVENT_TYPE_NONE:
+          //receiverThread_->yield();
+          usleep(1000);
           break;
-        case ENET_EVENT_TYPE_NONE:
-          receiverThread_->yield();
-          break;
       }
 //       usleep(100);
-      receiverThread_->yield(); //TODO: find apropriate
+      //receiverThread_->yield(); //TODO: find apropriate
     }
     disconnectClients();
     // if we're finishied, destroy server
-    enet_host_destroy(server);
+    {
+      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
+      enet_host_destroy(server);
+      lock.unlock();
+    }
   }
   
@@ -226,11 +258,15 @@
     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);
+        lock.unlock();
+      }
       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)
       {
@@ -258,29 +294,5 @@
   }
 
-  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
-NOTE: if you change this, don't forget to change the test function
-addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid
-*/
-  bool ConnectionManager::addClient(ENetEvent *event) {
-    ClientInformation *temp = head_->insertBack(new ClientInformation);
-    if(!temp){
-      COUT(2) << "Conn.Man. could not add client" << std::endl;
-      return false;
-    }
-    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
-      temp->prev()->setID(0); //bugfix: not necessary but usefull
-      temp->setID(1);
-    }
-    else
-      temp->setID(temp->prev()->getID()+1);
-    temp->setPeer(event->peer);
-    COUT(3) << "Con.Man: added client id: " << temp->getID() << std::endl;
-    return true;
-  }
+
 
   int ConnectionManager::getClientID(ENetPeer peer) {
@@ -316,73 +328,9 @@
       ++it;
     }
-    sendPackets();
+    //sendPackets();
     COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
   }
 
-  bool ConnectionManager::createClient(int clientID){
-    ClientInformation *temp = head_->findClient(clientID);
-    if(!temp){
-      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
-      return false;
-    }
-    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
-    syncClassid(temp->getID());
-    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
-    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
-    if(!createShip(temp))
-      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
-    else
-      COUT(3) << "created spaceship" << std::endl;
-    temp->setSynched(true);
-    COUT(3) << "sending welcome" << std::endl;
-    sendWelcome(temp->getID(), temp->getShipID(), true);
-    return true;
-  }
-  
-  bool ConnectionManager::removeClient(int clientID){
-    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
-    ClientInformation *client = head_->findClient(clientID);
-    if(!client)
-      return false;
-    while(it){
-      if(it->objectID!=client->getShipID()){
-        ++it;
-        continue;
-      }
-      orxonox::Iterator<orxonox::SpaceShip> temp=it;
-      ++it;
-      delete  *temp;
-      return head_->removeClient(clientID);
-    }
-    return false;
-  }
-  
-  bool ConnectionManager::createShip(ClientInformation *client){
-    if(!client)
-      return false;
-    orxonox::Identifier* id = ID("SpaceShip");
-    if(!id){
-      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
-      return false;
-    }
-    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
-    no->setPosition(orxonox::Vector3(0,80,0));
-    no->setScale(10);
-    no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
-    no->setMesh("assff.mesh");
-    no->setMaxSpeed(500);
-    no->setMaxSideAndBackSpeed(50);
-    no->setMaxRotation(1.0);
-    no->setTransAcc(200);
-    no->setRotAcc(3.0);
-    no->setTransDamp(75);
-    no->setRotDamp(1.0);
-    no->setCamera("cam_"+client->getID());
-    no->classID = id->getNetworkID();
-    no->create();
-    
-    client->setShipID(no->objectID);
-    return true;
-  }
+  
   
   bool ConnectionManager::removeShip(ClientInformation *client){
@@ -399,5 +347,5 @@
   bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){
     if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){
-      sendPackets();
+      //sendPackets();
       return true;
     }else
@@ -406,5 +354,9 @@
   
   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);
+      lock.unlock();
+    }
     removeShip(client);
   }
@@ -418,36 +370,4 @@
   
   
-//   int ConnectionManager::getNumberOfClients() {
-//     
-//     return clientsShip.size();
-//   }
-  
-  /*void ConnectionManager::addClientsObjectID( int clientID, int objectID ) {
-  COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl;
-  clientsShip.insert( std::make_pair( clientID, objectID ) );
+
 }
-
-  int ConnectionManager::getClientsShipID( int clientID ) {
-  return clientsShip[clientID];
-}
-
-  int ConnectionManager::getObjectsClientID( int objectID ) {
-  std::map<int, int>::iterator iter;
-  for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
-  if( iter->second == objectID ) return iter->first;
-}
-  return -99;
-}
-
-  void ConnectionManager::deleteClientIDReg( int clientID ) {
-  clientsShip.erase( clientID );
-}
-
-  void ConnectionManager::deleteObjectIDReg( int objectID ) {
-  std::map<int, int>::iterator iter = clientsShip.begin();
-  for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
-  if( iter->second == objectID ) break;
-}
-  clientsShip.erase( iter->first );
-}*/
-}
Index: code/trunk/src/network/ConnectionManager.h
===================================================================
--- code/trunk/src/network/ConnectionManager.h	(revision 1360)
+++ code/trunk/src/network/ConnectionManager.h	(revision 1502)
@@ -48,4 +48,5 @@
 #include <enet/enet.h>
 #include <boost/thread/thread.hpp>
+#include <boost/thread/recursive_mutex.hpp>
 
 #include "PacketBuffer.h"
@@ -61,6 +62,5 @@
 #define NETWORK_PORT 55556
 #define NETWORK_MAX_CONNECTIONS 50
-#define NETWORK_WAIT_TIMEOUT 5000
-#define NETWORK_SEND_WAIT 5
+#define NETWORK_WAIT_TIMEOUT 1
 
   struct ClientList{
@@ -74,8 +74,10 @@
     ConnectionManager();
     ConnectionManager(ClientInformation *head);
+    ConnectionManager(ClientInformation *head, int port);
     ConnectionManager(int port, const char *address, ClientInformation *head);
     ConnectionManager(int port, std::string address, ClientInformation *head);
-    ENetPacket *getPacket(ENetAddress &address); // thread1
-    ENetPacket *getPacket(int &clientID);
+    //ENetPacket *getPacket(ENetAddress &address); // thread1
+    //ENetPacket *getPacket(int &clientID);
+    ENetEvent *getEvent();
     bool queueEmpty();
     void createListener();
@@ -84,23 +86,23 @@
     bool addPacket(ENetPacket *packet, int ID);
     bool addPacketAll(ENetPacket *packet);
-    bool sendPackets(ENetEvent *event);
+  //  bool sendPackets(ENetEvent *event);
     bool sendPackets();
-    bool createClient(int clientID);
+    //bool createClient(int clientID);
     void disconnectClient(ClientInformation *client);
+    void syncClassid(int clientID);
+    bool sendWelcome(int clientID, int shipID, bool allowed);
 
   private:
-    bool clientDisconnect(ENetPeer *peer);
-    bool removeClient(int clientID);
+//     bool clientDisconnect(ENetPeer *peer);
+//     bool removeClient(int clientID);
     bool processData(ENetEvent *event);
-    bool addClient(ENetEvent *event);
+    //bool addClient(ENetEvent *event);
     void receiverThread();
     void disconnectClients();
     int getClientID(ENetPeer peer);
     int getClientID(ENetAddress address);
-    void syncClassid(int clientID);
     ENetPeer *getClientPeer(int clientID);
-    bool createShip(ClientInformation *client);
+    //bool createShip(ClientInformation *client);
     bool removeShip(ClientInformation *client);
-    bool sendWelcome(int clientID, int shipID, bool allowed);
     bool addFakeConnectRequest(ENetEvent *ev);
     PacketBuffer buffer;
@@ -114,4 +116,5 @@
 
     boost::thread *receiverThread_;
+    static boost::recursive_mutex enet_mutex_;
 //     int getNumberOfClients();
     //functions to map what object every clients uses
Index: code/trunk/src/network/GameStateClient.cc
===================================================================
--- code/trunk/src/network/GameStateClient.cc	(revision 1360)
+++ code/trunk/src/network/GameStateClient.cc	(revision 1502)
@@ -21,7 +21,7 @@
  *
  *   Author:
- *      ...
+ *      Oliver Scheuss
  *   Co-authors:
- *      ...
+ *      Dumeni Manatschal
  *
  */
@@ -35,5 +35,4 @@
 #include "Synchronisable.h"
 
-#define GAMESTATEID_INITIAL -1
 
 namespace network
@@ -43,9 +42,11 @@
     int id;
   };
-  
+
   GameStateClient::GameStateClient() {
     COUT(5) << "this: " << this << std::endl;
     last_diff_=0;
     last_gamestate_=GAMESTATEID_INITIAL-1;
+    tempGameState_=NULL;
+    myShip_=NULL;
   }
 
@@ -95,7 +96,9 @@
     return false;
   }
-  
+
   GameStateCompressed *GameStateClient::popPartialGameState(){
     GameState *gs = getPartialSnapshot();
+    if(!gs)
+      return NULL;
     GameStateCompressed *cgs = compress_(gs);
     delete[] gs->data;
@@ -103,5 +106,29 @@
     return cgs;
   }
-  
+
+  void GameStateClient::addGameState(GameStateCompressed *gs){
+    if(tempGameState_!=NULL){
+      //delete the obsolete gamestate
+      if(tempGameState_->id>gs->id)
+        return;
+      delete[] tempGameState_->data;
+      delete tempGameState_;
+    }
+    tempGameState_=gs;
+  }
+  int GameStateClient::processGameState(){
+    if(tempGameState_==NULL)
+      return 0;
+    int id=tempGameState_->id;
+    bool b = saveShipCache();
+    if(pushGameState(tempGameState_)){
+      if(b)
+        loadShipCache();
+      return id;
+    }
+    else
+      return GAMESTATEID_INITIAL;
+  }
+
 
   /**
@@ -151,6 +178,6 @@
           orxonox::Identifier* id = ID((unsigned int)sync.classID);
           if(!id){
-            COUT(4) << "We could not identify a new object; classid: " << sync.classID << std::endl;
-            return false;
+            COUT(3) << "We could not identify a new object; classid: " << sync.classID << " uint: " << (unsigned int)sync.classID << " objectID: " << sync.objectID << " size: " << sync.length << std::endl;
+            return false; // most probably the gamestate is corrupted
           }
           Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate());
@@ -168,5 +195,7 @@
           }
           if( !no->create() )
+          {
             COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
+          }
           it=orxonox::ObjectList<Synchronisable>::end();
         }
@@ -174,6 +203,8 @@
         // we have our object
         if(! it->updateData(sync))
+        {
           COUT(1) << "We couldn't update objectID: " \
           << sync.objectID << "; classID: " << sync.classID << std::endl;
+        }
       }
       ++it;
@@ -213,4 +244,6 @@
     }
     //retval->data = (unsigned char*)malloc(size);
+    if(size==0)
+      return NULL;
     retval->data = new unsigned char[size];
     if(!retval->data){
@@ -245,6 +278,6 @@
     return retval;
   }
-  
-  
+
+
   GameState *GameStateClient::undiff(GameState *old, GameState *diff) {
     if(!old || !diff)
@@ -253,9 +286,11 @@
     int of=0; // pointers offset
     int dest_length=0;
-    if(old->size>=diff->size)
+    /*if(old->size>=diff->size)
       dest_length=old->size;
-    else
+    else*/
       dest_length=diff->size;
 //     unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
+    if(dest_length==0)
+      return NULL;
     unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
     while(of<old->size && of<diff->size){
@@ -270,10 +305,10 @@
           of++;
         }
-      } else{
+      } /*else{
         while(of<dest_length){
           *(dp+of)=*(ap+of)^n;
           of++;
         }
-      }
+      }*/
     }
     // should be finished now
@@ -297,4 +332,6 @@
 
     uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
+    if(buffer==0)
+      return NULL;
     unsigned char *dest = new unsigned char[buffer];
     int retval;
@@ -303,5 +340,5 @@
     switch ( retval ) {
       case Z_OK: COUT(5) << "G.St.Cl: compress: successfully compressed" << std::endl; break;
-      case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 
+      case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl;
       return NULL;
       case Z_BUF_ERROR: COUT(2) << "G.St.Cl: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
@@ -321,6 +358,6 @@
     return compressedGamestate;
   }
-  
-  
+
+
   GameState *GameStateClient::decompress(GameStateCompressed *a) {
     //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
@@ -333,4 +370,6 @@
       bufsize = normsize;
 //     unsigned char* dest = (unsigned char*)malloc( bufsize );
+    if(bufsize==0)
+      return NULL;
     unsigned char *dest = new unsigned char[bufsize];
     int retval;
@@ -376,5 +415,5 @@
     return t;
   }
-  
+
   void GameStateClient::cleanup(){
     std::map<int, GameState*>::iterator temp, it = gameStateMap.begin();
@@ -388,4 +427,5 @@
       gameStateMap.erase(temp);
     }
+    tempGameState_=NULL;
   }
 
@@ -397,19 +437,44 @@
     }
     COUT(4) << std::endl;
-    
-  }
-  
-  
+
+  }
+
+  bool GameStateClient::saveShipCache(){
+    if(myShip_==NULL)
+      myShip_ = orxonox::SpaceShip::getLocalShip();
+    if(myShip_){
+      //      unsigned char *data = new unsigned char[myShip_->getSize()];
+      int size=myShip_->getSize(0x3);
+      if(size==0)
+        return false;
+      unsigned char *data = new unsigned char [size];
+      shipCache_ = myShip_->getData(data, 0x1);
+      return true;
+    }else
+      return false;
+  }
+
+  bool GameStateClient::loadShipCache(){
+    if(myShip_){
+      myShip_->updateData(shipCache_, 0x2);
+      if(shipCache_.data){
+        delete[] shipCache_.data;
+      }
+      return true;
+    }else
+      return false;
+  }
+
     //##### ADDED FOR TESTING PURPOSE #####
   GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
     return decompress( gc );
   }
-  
+
   GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
     return undiff( g_old, g_diffed );
   }
   //##### ADDED FOR TESTING PURPOSE #####
-  
-  
+
+
 }
 
Index: code/trunk/src/network/GameStateClient.h
===================================================================
--- code/trunk/src/network/GameStateClient.h	(revision 1360)
+++ code/trunk/src/network/GameStateClient.h	(revision 1502)
@@ -21,7 +21,7 @@
  *
  *   Author:
- *      ...
+ *      Oliver Scheuss
  *   Co-authors:
- *      ...
+ *      Dumeni Manatschal
  *
  */
@@ -33,5 +33,5 @@
 //
 //
-// Author:  <>, (C) 2007
+// Author:  Oliver Scheuss, (C) 2007
 //
 // Copyright: See COPYING file that comes with this distribution
@@ -46,5 +46,8 @@
 #include "core/CorePrereqs.h"
 #include "PacketTypes.h"
+#include "objects/SpaceShip.h"
 
+
+#define GAMESTATEID_INITIAL -1
 
 namespace network
@@ -56,10 +59,12 @@
     ~GameStateClient();
     
+    void addGameState(GameStateCompressed *gs);
+    int processGameState();
+    GameStateCompressed *popPartialGameState();
+    void cleanup();
+  private:
     bool pushGameState(GameStateCompressed *compstate);
-    GameStateCompressed *popPartialGameState();
-  private:
     bool loadSnapshot(GameState *state);
     GameState *getPartialSnapshot();
-    void cleanup();
     GameState *undiff(GameState *old, GameState *diff);
     GameStateCompressed *compress_(GameState *a);
@@ -69,8 +74,13 @@
     void removeObject(orxonox::Iterator<Synchronisable> &it);
     void printGameStateMap();
+    bool saveShipCache();
+    bool loadShipCache();
 
     int           last_diff_;
     int           last_gamestate_;
     std::map<int, GameState *> gameStateMap;
+    GameStateCompressed *tempGameState_; // we save the received gamestates here during processQueue
+    orxonox::SpaceShip *myShip_;
+    syncData shipCache_;
     
     
Index: code/trunk/src/network/GameStateManager.cc
===================================================================
--- code/trunk/src/network/GameStateManager.cc	(revision 1360)
+++ code/trunk/src/network/GameStateManager.cc	(revision 1502)
@@ -68,4 +68,27 @@
     printGameStates();
     return;
+  }
+  
+  void GameStateManager::addGameState(GameStateCompressed *gs, int clientID){
+    if(!gs)
+      return;
+    std::map<int, GameStateCompressed*>::iterator it = gameStateQueue.find(clientID);
+    if(it!=gameStateQueue.end()){
+      // delete obsolete gamestate
+      delete[] it->second->data;
+      delete it->second;
+    }
+    gameStateQueue[clientID] = gs;
+    return;
+  }
+  
+  void GameStateManager::processGameStates(){
+    std::map<int, GameStateCompressed*>::iterator it;
+    // now push only the most recent gamestates we received (ignore obsolete ones)
+    for(it = gameStateQueue.begin(); it!=gameStateQueue.end(); it++){
+      pushGameState(it->second, it->first);
+    }
+    // now clear the queue
+    gameStateQueue.clear();
   }
   
@@ -117,5 +140,5 @@
         client = it->second;
       GameState *server = reference;
-      COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl;
+      COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl;
       if(client)
         return encode(client, server);
@@ -170,4 +193,6 @@
     }
     //retval->data = (unsigned char*)malloc(size);
+    if(size==0)
+      return NULL;
     retval->data = new unsigned char[size];
     if(!retval->data){
@@ -237,4 +262,6 @@
       data+=sizeof(int);
       sync.classID = *(int*)data;
+      if(sync.classID == 0) // TODO: remove this
+        COUT(3) << "received a classid 0" << std::endl;
       data+=sizeof(int);
       sync.data = data;
@@ -246,5 +273,5 @@
 
       if(!it){
-        // the object does not exist yet
+        // the objectaber ich glaub die  does not exist yet
         COUT(4) << "loadsnapshot: creating new object " << std::endl;
         //COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
@@ -314,38 +341,40 @@
   }
 
-  GameState *GameStateManager::diff(GameState *a, GameState *b) {
-    unsigned char *ap = a->data, *bp = b->data;
+  GameState *GameStateManager::diff(GameState *alt, GameState *neu) {
+    unsigned char *ap = alt->data, *bp = neu->data;
     int of=0; // pointers offset
     int dest_length=0;
-    if(a->size>=b->size)
-      dest_length=a->size;
-    else
-      dest_length=b->size;
+    /*if(alt->size>neu->size)
+      dest_length=alt->size;
+    else*/
+      dest_length=neu->size;
+    if(dest_length==0)
+      return NULL;
     //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
     unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
-    while(of<a->size && of<b->size){
+    while(of<alt->size && of<neu->size){
       *(dp+of)=*(ap+of)^*(bp+of); // do the xor
       ++of;
     }
-    if(a->size!=b->size){ // do we have to fill up ?
+    if(alt->size!=neu->size){ // do we have to fill up ?
       unsigned char n=0;
-      if(a->size<b->size){
+      if(alt->size<neu->size){
         while(of<dest_length){
           *(dp+of)=n^*(bp+of);
           of++;
         }
-      } else{
+      } /*else{
         while(of<dest_length){
           *(dp+of)=*(ap+of)^n;
           of++;
         }
-      }
+      }*/
     }
 
     GameState *r = new GameState;
-    r->id = b->id;
+    r->id = neu->id;
     r->size = dest_length;
     r->diffed = true;
-    r->base_id = a->id;
+    r->base_id = alt->id;
     r->data = dp;
     r->complete = true;
@@ -362,4 +391,6 @@
     //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
     //unsigned char* dest = (unsigned char*)malloc( buffer );
+    if(buffer==0)
+      return NULL;
     unsigned char *dest = new unsigned char[buffer];
     //COUT(4) << "dest: " << dest << std::endl;
@@ -404,4 +435,6 @@
       bufsize = normsize;
 //     unsigned char* dest = (unsigned char*)malloc( bufsize );
+    if(bufsize==0)
+      return NULL;
     unsigned char *dest = new unsigned char[bufsize];
     int retval;
@@ -436,4 +469,11 @@
       return;
     int curid = temp->getGamestateID();
+    
+    if(gamestateID == GAMESTATEID_INITIAL){
+      temp->setGameStateID(GAMESTATEID_INITIAL);
+      if(curid!=GAMESTATEID_INITIAL)
+        --(gameStateUsed.find(curid)->second);
+      return;
+    }
     if(curid > gamestateID)
       // the network packets got messed up 
@@ -446,5 +486,5 @@
       --(gameStateUsed.find(curid)->second);
     ++(gameStateUsed.find(gamestateID)->second);
-    temp->setGamestateID(gamestateID);
+    temp->setGameStateID(gamestateID);
     /*
     GameState *old = clientGameState[clientID];
Index: code/trunk/src/network/GameStateManager.h
===================================================================
--- code/trunk/src/network/GameStateManager.h	(revision 1360)
+++ code/trunk/src/network/GameStateManager.h	(revision 1502)
@@ -72,10 +72,13 @@
     ~GameStateManager();
     
+    void addGameState(GameStateCompressed *gs, int clientID);
+    void processGameStates();
+    
     void update();
     GameStateCompressed *popGameState(int clientID);
-    bool pushGameState(GameStateCompressed *gs, int clientID);
     void ackGameState(int clientID, int gamestateID);
     void removeClient(ClientInformation *client);
-  private:
+    private:
+    bool pushGameState(GameStateCompressed *gs, int clientID);
     void cleanup(); // "garbage handler"
     GameState *getSnapshot();
@@ -83,5 +86,5 @@
     GameStateCompressed *encode(GameState *a, GameState *b);
     GameStateCompressed *encode(GameState *a);
-    GameState *diff(GameState *a, GameState *b);
+    GameState *diff(GameState *alt, GameState *neu);
     GameStateCompressed *compress_(GameState *a);
     GameState *decompress(GameStateCompressed *a);
@@ -91,4 +94,5 @@
     std::map<int, GameState*> gameStateMap; //map gsID to gamestate*
     std::map<int, int> gameStateUsed; // save the number of clients, that use the specific gamestate
+    std::map<int, GameStateCompressed*> gameStateQueue;
     GameState *reference;
     ClientInformation *head_;
Index: code/trunk/src/network/NetworkPrereqs.h
===================================================================
--- code/trunk/src/network/NetworkPrereqs.h	(revision 1360)
+++ code/trunk/src/network/NetworkPrereqs.h	(revision 1502)
@@ -35,5 +35,5 @@
 #define _NetworkPrereqs_H__
 
-#include "OrxonoxPlatform.h"
+#include "util/OrxonoxPlatform.h"
 
 //-----------------------------------------------------------------------
@@ -54,20 +54,4 @@
 #else
 #  define _NetworkExport
-#endif
-
-//-----------------------------------------------------------------------
-// fixed width integers
-//-----------------------------------------------------------------------
-#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC
-typedef __int8            int8_t;
-typedef __int16           int16_t;
-typedef __int32           int32_t;
-typedef __int64           int64_t;
-typedef unsigned __int8   uint8_t;
-typedef unsigned __int16  uint16_t;
-typedef unsigned __int32  uint32_t;
-typedef unsigned __int64  uint64_t;
-#else
-# include "inttypes.h"
 #endif
 
Index: code/trunk/src/network/PacketBuffer.cc
===================================================================
--- code/trunk/src/network/PacketBuffer.cc	(revision 1360)
+++ code/trunk/src/network/PacketBuffer.cc	(revision 1502)
@@ -62,6 +62,5 @@
       last->next=NULL;
       // change this!!!!!!!  ---- we are not doing stl so we won't change this
-      last->packet = ev->packet;
-      last->address = ev->peer->address;
+      last->event = ev;
       //last->address = ev->peer->address;
     } else {
@@ -72,6 +71,5 @@
       last->next=NULL;
       // save the packet to the new element
-      last->packet = ev->packet;
-      last->address = ev->peer->address;
+      last->event = ev;
       //last->address = ev->peer->address;
     }
@@ -82,10 +80,10 @@
   //returns the first element in the list without deleting it but
   //moving first pointer to next element
-  ENetPacket *PacketBuffer::pop() {
+  /*ENetPacket *PacketBuffer::pop() {
     ENetAddress address;
     return pop(address);
-  }
-
-  ENetPacket *PacketBuffer::pop(ENetAddress &address) {
+  }*/
+  
+  ENetEvent *PacketBuffer::pop(){
     boost::recursive_mutex::scoped_lock lock(mutex_);
     //std::cout << "packetbuffer pop(address)" << std::endl;
@@ -93,6 +91,26 @@
       QueueItem *temp = first;
       // get packet
-      ENetPacket *pck=first->packet;
-      address = first->address;
+      ENetEvent *ev=first->event;
+      //address = first->address;
+      // remove first element
+      first = first->next;
+      delete temp;
+      lock.unlock();
+      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
+      return ev;
+    } else{
+      lock.unlock();
+      return NULL;
+    }
+  }
+
+  /*ENetPacket *PacketBuffer::pop(ENetAddress &address) {
+    boost::recursive_mutex::scoped_lock lock(mutex_);
+    //std::cout << "packetbuffer pop(address)" << std::endl;
+    if(first!=NULL ){
+      QueueItem *temp = first;
+      // get packet
+      ENetPacket *pck=first->event->packet;
+      address = first->event->peer->address;
       // remove first element
       first = first->next;
@@ -105,5 +123,5 @@
       return NULL;
     }
-  }
+  }*/
 
   bool PacketBuffer::isEmpty() {
Index: code/trunk/src/network/PacketBuffer.h
===================================================================
--- code/trunk/src/network/PacketBuffer.h	(revision 1360)
+++ code/trunk/src/network/PacketBuffer.h	(revision 1502)
@@ -55,6 +55,6 @@
 
   struct QueueItem{
-    ENetPacket *packet;
-    ENetAddress address;
+    ENetEvent *event;
+    //ENetAddress address;
     QueueItem *next;
   };
@@ -68,6 +68,7 @@
     void print();
     // pops a packet from the queue
-    ENetPacket *pop();
-    ENetPacket *pop(ENetAddress &address);
+    //ENetPacket *pop();
+    //ENetPacket *pop(ENetAddress &address);
+    ENetEvent *pop();
     // pushs a packet to the queue
     bool push(ENetEvent *ev);
Index: code/trunk/src/network/PacketDecoder.cc
===================================================================
--- code/trunk/src/network/PacketDecoder.cc	(revision 1360)
+++ code/trunk/src/network/PacketDecoder.cc	(revision 1502)
@@ -68,10 +68,4 @@
     case COMMAND:
       return command( packet, clientId );
-    case MOUSE:
-      mousem( packet, clientId );
-      return true;
-    case KEYBOARD:
-      keystrike( packet, clientId );
-      return true;
     case CHAT:
       chatMessage( packet, clientId );
@@ -90,5 +84,5 @@
     return false;
   }
-  
+
   bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){
     uint32_t submittetcrc;
@@ -108,5 +102,5 @@
     return false;
   }
-  
+
   // ATTENTION: TODO watch, that arguments we pass over to the processFunction gets deleted in THE PROCESSXXX function
 
@@ -124,7 +118,9 @@
     enet_packet_destroy( packet );
   }
-  
+
   bool PacketDecoder::command( ENetPacket* packet, int clientId ){
     int length = *(int*)((unsigned char *)packet->data+sizeof(int));
+    if(length<=0)
+      return false;
     void *data = (void *)new unsigned char[length];
     memcpy(data, (void *)(packet->data+2*sizeof(int)), length);
@@ -133,29 +129,9 @@
   }
 
-  void PacketDecoder::mousem( ENetPacket* packet, int clientId )
-  {
-    mouse* mouseMove = new mouse;
-    //copy data of packet->data to new struct
-    *mouseMove = *(mouse*)packet->data;
-
-    //clean memory
-    enet_packet_destroy( packet );
-    printMouse( mouseMove ); //debug info
-  }
-
-  void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
-  {
-    keyboard* key = new keyboard;
-    *key = *(keyboard*)packet->data; //see above
-
-    //clean memory
-    enet_packet_destroy( packet );
-    printKey( key ); //debug info
-
-  }
-
   void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
   {
     chat* chatting = new chat;
+    if(packet->dataLength==4)
+      return;
     chatting->id = (int)*packet->data; //first copy id into new struct
     //since the chat message is a char*, allocate the memory needed
@@ -185,7 +161,4 @@
       return;
     }
-    //since it's not alowed to use void* for pointer arithmetic
-    //FIXME: variable never used
-    unsigned char* data = (unsigned char *)(packet->data);
     //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
     memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
@@ -202,9 +175,15 @@
     //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
     if(currentState->compsize==0)
+    {
       COUT(2) << "PacketDecoder: compsize is 0" << std::endl;
+    }
 //     currentState->data = (unsigned char*)(malloc( currentState->compsize ));
+    if(currentState->compsize==0)
+      return;
     currentState->data = new unsigned char[currentState->compsize];
     if(currentState->data==NULL)
+    {
       COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl;
+    }
     //copy the GameStateCompressed data
     memcpy( (void*)(currentState->data), (const void*)(packet->data+5*sizeof( int ) + 2*sizeof(bool)), currentState->compsize );
@@ -221,4 +200,6 @@
     cid->id = ((classid *)(packet->data))->id;
     cid->clid = ((classid *)(packet->data))->clid;
+    if(cid->length==0)
+      return;
 //     cid->message = (const char *)malloc(cid->length);
     cid->message = new char[cid->length];
@@ -229,6 +210,6 @@
     processClassid(cid);
   }
-  
-  
+
+
   bool PacketDecoder::decodeWelcome( ENetPacket* packet, int clientID ){
     welcome *w = new welcome;
@@ -240,5 +221,5 @@
     return processWelcome(w);
   }
-  
+
   bool PacketDecoder::decodeConnectRequest( ENetPacket *packet, int clientID ){
     connectRequest *con = new connectRequest;
@@ -279,10 +260,10 @@
     return;
   }
-  
+
   bool PacketDecoder::processWelcome( welcome *w ){
     delete w;
     return true;
   }
-  
+
   bool PacketDecoder::processConnectRequest( connectRequest *con, int clientID ){
     COUT(3) << "packetdecoder: processing connectRequest" << std::endl;
@@ -299,20 +280,11 @@
   }
 
-  void PacketDecoder::printMouse( mouse* data )
-  {
-    COUT(5) << "data id: " << data->id << std::endl;
-    COUT(5) << "data:    " << data->x << " " << data->y << std::endl;
-  }
-
-  void PacketDecoder::printKey( keyboard* data )
-  {
-    COUT(5) << "data id: " << data->id << std::endl;
-    COUT(5) << "data:    " << (char)data->press << std::endl;
-  }
 
   void PacketDecoder::printChat( chat* data, int clientId )
   {
     if(clientId!=CLIENTID_CLIENT)
+    {
       COUT(5) << "client: " << clientId << std::endl;
+    }
     COUT(5) << "data id: " << data->id << std::endl;
     COUT(5) << "data:    " << data->message << std::endl;
Index: code/trunk/src/network/PacketGenerator.cc
===================================================================
--- code/trunk/src/network/PacketGenerator.cc	(revision 1360)
+++ code/trunk/src/network/PacketGenerator.cc	(revision 1502)
@@ -78,10 +78,10 @@
   {
     COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
-    ack* ackreq = new ack;
+    
+    ENetPacket *packet = enet_packet_create( NULL , sizeof( ack ), reliable );
+    ack* ackreq = (ack *)packet->data;
     ackreq->id = ACK;
     ackreq->a = state;
-
-    ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
-    delete ackreq;
+    //delete ackreq;
     return packet;
   }
@@ -89,4 +89,6 @@
   ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE )
   {
+    if(dataLength==0)
+      return NULL;
     unsigned char *stream = new unsigned char[dataLength + 2*sizeof(int)];
     if(!stream)
@@ -101,30 +103,4 @@
   }
 
-  /*### mouseupdates */
-  ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
-  {
-    COUT(4) << "PacketGenerator: generating new mouse" << std::endl;
-    mouse* mousemove = new mouse;
-    mousemove->id = MOUSE;
-    mousemove->x = x;
-    mousemove->y = y;
-
-    ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
-    delete mousemove;
-    return packet;
-  }
-
-  /*### keystrikes updates */
-  ENetPacket* PacketGenerator::keystrike( char press, int reliable )
-  {
-    COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;
-    keyboard* key = new keyboard;
-    key->id = KEYBOARD;
-    key->press = press;
-
-    ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
-    delete key;
-    return packet;
-  }
 
   /*### chat messages packet */
@@ -150,5 +126,9 @@
     //std::cout << "totalLen " << totalLen << std::endl;
     //unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
-    unsigned char *data = new unsigned char[totalLen];
+    if(totalLen==0)
+      return NULL;
+    ENetPacket *packet = enet_packet_create( NULL , totalLen+sizeof(uint32_t), reliable );
+    //unsigned char *data = new unsigned char[totalLen];
+    unsigned char *data = packet->data;
     memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
     memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
@@ -163,6 +143,5 @@
     //create an enet packet with the generated bytestream
     COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
-    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
-    delete[] data;
+    //delete[] data;
     if(!addCRC(packet))
       COUT(3) << "could not add crc to gamestate packet" << std::endl;
@@ -173,4 +152,6 @@
   {
     //unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
+    if(classname.length()==0)
+      return NULL;
     unsigned char *data = new unsigned char[3*sizeof(int)+classname.length()+1];
     std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl;
@@ -206,14 +187,14 @@
   bool PacketGenerator::addCRC( ENetPacket *packet){
     unsigned char *data = packet->data;
-    uint32_t crc32=calcCRC(data, packet->dataLength);
+    uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t));
     // now append the crc to the packet data
-    int oldlength = packet->dataLength;
-    if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
+    int oldlength = packet->dataLength-sizeof(uint32_t);
+    //if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
       memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
       return true;
-    }else{
-      COUT(3) << "could not add crc to gamestate" << std::endl;
-      return false;
-    }
+    //}else{
+     // COUT(3) << "could not add crc to gamestate" << std::endl;
+     // return false;
+    //}
   }
 
Index: code/trunk/src/network/PacketManager.h
===================================================================
--- code/trunk/src/network/PacketManager.h	(revision 1360)
+++ code/trunk/src/network/PacketManager.h	(revision 1502)
@@ -59,6 +59,4 @@
     ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability
     ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE );
-    ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE );
-    ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );
     ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE );
     ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates
@@ -94,6 +92,4 @@
     void acknowledgement( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
     bool command( ENetPacket* packet, int clientId );
-    void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
-    void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
     void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
     void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT );
@@ -113,6 +109,4 @@
     //print functions
     void printAck( ack* data );
-    void printMouse( mouse* data );
-    void printKey( keyboard* data );
     void printChat( chat* data, int clientId );
     void printGamestate( GameStateCompressed *data );
Index: code/trunk/src/network/PacketTypes.h
===================================================================
--- code/trunk/src/network/PacketTypes.h	(revision 1360)
+++ code/trunk/src/network/PacketTypes.h	(revision 1502)
@@ -48,7 +48,5 @@
   enum packet_id {
     ACK,
-    MOUSE,
     COMMAND,
-    KEYBOARD,
     CHAT,
     GAMESTATE ,
@@ -104,14 +102,4 @@
   };
 
-  struct mouse {
-    int id;
-    double x;
-    double y;
-  };
-
-  struct keyboard {
-    int id;
-    char press;
-  };
   //only in this class, not PacketGenerator, used as pattern to put incoming
   //bytes inside
Index: code/trunk/src/network/Server.cc
===================================================================
--- code/trunk/src/network/Server.cc	(revision 1360)
+++ code/trunk/src/network/Server.cc	(revision 1502)
@@ -42,4 +42,5 @@
 
 #include <iostream>
+
 
 #include "ConnectionManager.h"
@@ -49,4 +50,5 @@
 //#include "NetworkFrameListener.h"
 #include "util/Sleep.h"
+#include "objects/SpaceShip.h"
 
 
@@ -56,5 +58,5 @@
   
 #define MAX_FAILURES 20;
-  
+#define NETWORK_FREQUENCY 30
   
   /**
@@ -63,4 +65,5 @@
   */
   Server::Server() {
+    timeSinceLastUpdate_=0;
     packet_gen = PacketGenerator();
     clients = new ClientInformation(true);
@@ -68,4 +71,12 @@
     gamestates = new GameStateManager(clients);
   }
+  
+  Server::Server(int port){
+    timeSinceLastUpdate_=0;
+    packet_gen = PacketGenerator();
+    clients = new ClientInformation(true);
+    connection = new ConnectionManager(clients, port);
+    gamestates = new GameStateManager(clients);
+  }
 
   /**
@@ -75,4 +86,5 @@
   */
   Server::Server(int port, std::string bindAddress) {
+    timeSinceLastUpdate_=0;
     packet_gen = PacketGenerator();
     clients = new ClientInformation();
@@ -87,4 +99,5 @@
   */
   Server::Server(int port, const char *bindAddress) {
+    timeSinceLastUpdate_=0;
     packet_gen = PacketGenerator();
     clients = new ClientInformation();
@@ -117,9 +130,5 @@
     ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
     //std::cout <<"adding packets" << std::endl;
-    if(connection->addPacketAll(packet))
-    //std::cout <<"added packets" << std::endl;
-      return connection->sendPackets();
-    else
-      return false;
+    return connection->addPacketAll(packet);
   }
 
@@ -132,11 +141,5 @@
     ENetPacket *packet = packet_gen.chatMessage(msg);
     COUT(4) <<"Server: adding Packets" << std::endl;
-    connection->addPacketAll(packet);
-    //std::cout <<"added packets" << std::endl;
-    if (connection->sendPackets()){
-      COUT(4) << "Server: Sucessfully" << std::endl;
-      return true;
-    }
-    return false;
+    return connection->addPacketAll(packet);
   }
 
@@ -148,6 +151,12 @@
   void Server::tick(float time) {
     processQueue();
-    updateGamestate();
-//     usleep(500000); // TODO remove
+    //this steers our network frequency
+    timeSinceLastUpdate_+=time;
+    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
+      timeSinceLastUpdate_-=(1./NETWORK_FREQUENCY);
+      gamestates->processGameStates();
+      updateGamestate();
+    }
+//     usleep(5000); // TODO remove
     return;
   }
@@ -157,15 +166,32 @@
   */
   void Server::processQueue() {
-    ENetPacket *packet;
+    ENetEvent *event;
     int clientID=-1;
     while(!connection->queueEmpty()){
       //std::cout << "Client " << clientID << " sent: " << std::endl;
       //clientID here is a reference to grab clientID from ClientInformation
-      packet = connection->getPacket(clientID);
-      if(!packet)
-        continue;
+      event = connection->getEvent();
+      if(!event)
+        continue;
+      assert(event->type != ENET_EVENT_TYPE_NONE);
+      switch( event->type ) {
+      case ENET_EVENT_TYPE_CONNECT:
+        COUT(3) << "processing event_Type_connect" << std::endl;
+        addClient(event);
+        break;
+      case ENET_EVENT_TYPE_DISCONNECT:
+        if(clients->findClient(&event->peer->address))
+          disconnectClient(event);
+        break;
+      case ENET_EVENT_TYPE_RECEIVE:
+        if(clients->findClient(&event->peer->address)){
+          clientID = clients->findClient(&event->peer->address)->getID();
+          if( !elaborate(event->packet, clientID) ) 
+            COUT(3) << "Server: could not elaborate" << std::endl;
+        }
+        break;
+      }
+      delete event;
       //if statement to catch case that packetbuffer is empty
-      if( !elaborate(packet, clientID) ) 
-        COUT(3) << "Server: could not elaborate" << std::endl;
     }
   }
@@ -210,14 +236,18 @@
       if(gs==NULL){
         COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
-        return false;
+        continue;
       }
       //std::cout << "adding gamestate" << std::endl;
-      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
+      ENetPacket *packet = packet_gen.gstate(gs);
+      if(!packet)
+	continue;
+      if ( !(connection->addPacket(packet, cid)) ){
         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
         temp->addFailure();
-        if(temp->getFailures() > 20 )
-          disconnectClient(temp);
+        /*if(temp->getFailures() > 0 )
+          disconnectClient(temp);*/
       //std::cout << "added gamestate" << std::endl;
-      }
+      }else
+        temp->resetFailures();
       added=true;
       temp=temp->next();
@@ -226,14 +256,14 @@
       delete gs;
     }
-    if(added) {
+    /*if(added) {
       //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
       return connection->sendPackets();
-    }
-    COUT(5) << "Server: had no gamestates to send" << std::endl;
-    return false;
+    }*/
+    //COUT(5) << "Server: had no gamestates to send" << std::endl;
+    return true;
   }
 
   void Server::processAck( ack *data, int clientID) {
-    COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
+    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
     gamestates->ackGameState(clientID, data->a);
     delete data;
@@ -241,7 +271,7 @@
   
   bool Server::processConnectRequest( connectRequest *con, int clientID ){
-    COUT(3) << "processing connectRequest " << std::endl;
+    //(COUT(3) << "processing connectRequest " << std::endl;
     //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
-    connection->createClient(clientID);
+    //createClient(clientID);
     delete con;
     return true;
@@ -250,9 +280,96 @@
   void Server::processGamestate( GameStateCompressed *data, int clientID){
     COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
-    if(!gamestates->pushGameState(data, clientID))
-        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
+    gamestates->addGameState(data, clientID);
+        /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
     else
       if(clients->findClient(clientID))
-        clients->findClient(clientID)->resetFailures();
+        clients->findClient(clientID)->resetFailures();*/
+  }
+  
+  bool Server::addClient(ENetEvent *event){
+    ClientInformation *temp = clients->insertBack(new ClientInformation);
+    if(!temp){
+      COUT(2) << "Server: could not add client" << std::endl;
+      return false;
+    }
+    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
+      temp->prev()->setID(0); //bugfix: not necessary but usefull
+      temp->setID(1);
+    }
+    else
+      temp->setID(temp->prev()->getID()+1);
+    temp->setPeer(event->peer);
+    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
+    return createClient(temp->getID());
+  }
+  
+  bool Server::createClient(int clientID){
+    ClientInformation *temp = clients->findClient(clientID);
+    if(!temp){
+      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
+      return false;
+    }
+    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
+    connection->syncClassid(temp->getID());
+    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
+    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
+    if(!createShip(temp))
+      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
+    else
+      COUT(3) << "created spaceship" << std::endl;
+    temp->setSynched(true);
+    COUT(3) << "sending welcome" << std::endl;
+    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
+    return true;
+  }
+  
+  bool Server::createShip(ClientInformation *client){
+    if(!client)
+      return false;
+    orxonox::Identifier* id = ID("SpaceShip");
+    if(!id){
+      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
+      return false;
+    }
+    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
+    no->setPosition(orxonox::Vector3(0,0,80));
+    no->setScale(10);
+    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
+    no->setMesh("assff.mesh");
+    no->setMaxSpeed(500);
+    no->setMaxSideAndBackSpeed(50);
+    no->setMaxRotation(1.0);
+    no->setTransAcc(200);
+    no->setRotAcc(3.0);
+    no->setTransDamp(75);
+    no->setRotDamp(1.0);
+    no->setCamera("cam_"+client->getID());
+    no->classID = id->getNetworkID();
+    no->create();
+    
+    client->setShipID(no->objectID);
+    return true;
+  }
+  
+  bool Server::disconnectClient(ENetEvent *event){
+    COUT(4) << "removing client from list" << std::endl;
+    //return removeClient(head_->findClient(&(peer->address))->getID());
+    
+    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
+    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
+    ClientInformation *client = clients->findClient(&event->peer->address);
+    if(!client)
+      return false;
+    while(it){
+      if(it->objectID!=client->getShipID()){
+        ++it;
+        continue;
+      }
+      orxonox::Iterator<orxonox::SpaceShip> temp=it;
+      ++it;
+      delete  *temp;
+      return clients->removeClient(event->peer);
+    }
+    return false;
   }
 
Index: code/trunk/src/network/Server.h
===================================================================
--- code/trunk/src/network/Server.h	(revision 1360)
+++ code/trunk/src/network/Server.h	(revision 1502)
@@ -60,4 +60,5 @@
   public:
     Server();
+    Server(int port);
     Server(int port, std::string bindAddress);
     Server(int port, const char *bindAddress);
@@ -71,4 +72,8 @@
     void updateGamestate();
   private:
+    bool addClient(ENetEvent *event);
+    bool createClient(int clientID);
+    bool createShip(ClientInformation *client);
+    bool disconnectClient(ENetEvent *event);
     void disconnectClient(int clientID);
     void disconnectClient( ClientInformation *client);
@@ -82,4 +87,5 @@
 
     ClientInformation *clients;
+    float timeSinceLastUpdate_;
   };
 
Index: code/trunk/src/network/Synchronisable.cc
===================================================================
--- code/trunk/src/network/Synchronisable.cc	(revision 1360)
+++ code/trunk/src/network/Synchronisable.cc	(revision 1502)
@@ -66,4 +66,10 @@
 
   Synchronisable::~Synchronisable(){
+  }
+  
+  bool Synchronisable::create(){
+    this->classID = this->getIdentifier()->getNetworkID();
+    COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
+    return true;
   }
   
@@ -151,6 +157,8 @@
   * @return data containing all variables and their sizes
   */
-  syncData Synchronisable::getData(unsigned char *mem){
+  syncData Synchronisable::getData(unsigned char *mem, int mode){
     //std::cout << "inside getData" << std::endl;
+    if(mode==0x0)
+      mode=state_;
     if(classID==0)
       COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
@@ -167,5 +175,5 @@
     for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
       //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
-      if( ((*i)->mode & state_) == 0 ){
+      if( ((*i)->mode & mode) == 0 ){
         COUT(5) << "not getting data: " << std::endl;
         continue;  // this variable should only be received
@@ -194,5 +202,7 @@
   * @return true/false
   */
-  bool Synchronisable::updateData(syncData vars){
+  bool Synchronisable::updateData(syncData vars, int mode){
+    if(mode==0x0)
+      mode=state_;
     unsigned char *data=vars.data;
     std::list<synchronisableVariable *>::iterator i;
@@ -203,7 +213,7 @@
     COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
     for(i=syncList->begin(); i!=syncList->end(); i++){
-      if( ((*i)->mode ^ state_) == 0 ){
+      if( ((*i)->mode ^ mode) == 0 ){
         COUT(5) << "synchronisable: not updating variable " << std::endl;
-        continue;  // this variable should only be updated
+        continue;  // this variable should only be set
       }
       COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
@@ -230,9 +240,11 @@
   * @return amount of bytes
   */
-  int Synchronisable::getSize(){
+  int Synchronisable::getSize(int mode){
     int tsize=0;
+    if(mode==0x0)
+      mode=state_;
     std::list<synchronisableVariable *>::iterator i;
     for(i=syncList->begin(); i!=syncList->end(); i++){
-      if( ((*i)->mode & state_) == 0 )
+      if( ((*i)->mode & mode) == 0 )
         continue;  // this variable should only be received, so dont add its size to the send-size
       switch((*i)->type){
Index: code/trunk/src/network/Synchronisable.h
===================================================================
--- code/trunk/src/network/Synchronisable.h	(revision 1360)
+++ code/trunk/src/network/Synchronisable.h	(revision 1502)
@@ -83,11 +83,11 @@
     void registerVar(void *var, int size, variableType t, int mode=1);
     //  syncData getData();
-    syncData getData(unsigned char *mem);
-    int getSize();
-    bool updateData(syncData vars);
+    syncData getData(unsigned char *mem, int mode=0x0);
+    int getSize(int mode=0x0);
+    bool updateData(syncData vars, int mode=0x0);
     void setBacksync(bool sync);
     bool getBacksync();
     virtual void registerAllVariables()=0;
-    virtual bool create()=0;
+    virtual bool create();
     static void setClient(bool b);
   protected:
Index: code/trunk/src/network/diffTest.cc
===================================================================
--- code/trunk/src/network/diffTest.cc	(revision 1360)
+++ code/trunk/src/network/diffTest.cc	(revision 1502)
@@ -549,5 +549,5 @@
 
   std::cout << "test setGamestateID" << std::endl;
-  temp->setGamestateID( 8 );
+  temp->setGameStateID( 8 );
   printClientInformationBox( temp );
 
@@ -559,5 +559,5 @@
   newInfo->setSynched( true );
   newInfo->setPeer( NULL );
-  toool->setGamestateID( 199 );
+  toool->setGameStateID( 199 );
   toool->setID( numberOfClients+1);
   toool->setSynched( true );
Index: code/trunk/src/network/dummyclient3.cc
===================================================================
--- code/trunk/src/network/dummyclient3.cc	(revision 1360)
+++ code/trunk/src/network/dummyclient3.cc	(revision 1502)
@@ -51,6 +51,11 @@
   Client client( str, PORT );
   if ( client.establishConnection() )
+  {
     COUT(3) << "connection established" << std::endl;
-  else COUT(0) << "problems establishing connection" << std::endl;
+  }
+  else
+  {
+    COUT(0) << "problems establishing connection" << std::endl;
+  }
   char message[10000];
   char signs[] = "abcdefghijklmnopqrstuvwxy";
