Index: trunk/src/lib/network/message_manager.cc
===================================================================
--- trunk/src/lib/network/message_manager.cc	(revision 8147)
+++ trunk/src/lib/network/message_manager.cc	(revision 8228)
@@ -17,4 +17,6 @@
 
 #include "message_manager.h"
+
+#include "network_stream.h"
 
 using namespace std;
@@ -312,5 +314,6 @@
          recieverType == RT_ALL_NOT_ME ||
          recieverType == RT_USER && it->first == reciever ||
-         recieverType == RT_NOT_USER && it->first != reciever
+         recieverType == RT_NOT_USER && it->first != reciever ||
+         recieverType == RT_SERVER && getNetworkStream()->isUserServer( it->first )
        )
     {
Index: trunk/src/lib/network/message_manager.h
===================================================================
--- trunk/src/lib/network/message_manager.h	(revision 8147)
+++ trunk/src/lib/network/message_manager.h	(revision 8228)
@@ -30,5 +30,6 @@
   TESTMESSAGEID = 1,
   MSGID_DELETESYNCHRONIZEABLE,
-  MSGID_PREFEREDTEAM
+  MSGID_PREFEREDTEAM,
+  MSGID_CHANGEPLAYERNAME
 };
 
@@ -40,5 +41,6 @@
   RT_ALL_ME,           //!< message is sent to all users
   RT_USER,             //!< message is only sent to reciever
-  RT_NOT_USER          //!< message is sent to all but reciever
+  RT_NOT_USER,         //!< message is sent to all but reciever
+  RT_SERVER            //!< message is sent to server only
 };
 
Index: trunk/src/lib/network/network_game_manager.cc
===================================================================
--- trunk/src/lib/network/network_game_manager.cc	(revision 8147)
+++ trunk/src/lib/network/network_game_manager.cc	(revision 8228)
@@ -112,4 +112,6 @@
   stats->setPlayableUniqueId( playable.getUniqueID() );
   stats->setModelFileName( playableModel );
+  
+  return true;
 }
 
@@ -122,6 +124,12 @@
 bool NetworkGameManager::signalLeftPlayer(int userID)
 {
-  delete PlayerStats::getStats( userID )->getPlayable();
-  delete PlayerStats::getStats( userID );
+  if ( PlayerStats::getStats( userID ) )
+  {
+    if ( PlayerStats::getStats( userID )->getPlayable() )
+      delete PlayerStats::getStats( userID )->getPlayable();
+    delete PlayerStats::getStats( userID );
+  }
+  
+  return true;
 }
 
@@ -165,9 +173,11 @@
         return true;
       }
-        
+      
       delete dynamic_cast<Synchronizeable*>(*it);
       return true;
     }
   }
+  
+  return true;
 }
 
@@ -210,4 +220,6 @@
   
   NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
+  
+  return true;
 }
 
@@ -220,5 +232,4 @@
   
   stats.setPreferedTeamId( teamId );
-  
 }
 
Index: trunk/src/lib/network/network_manager.cc
===================================================================
--- trunk/src/lib/network/network_manager.cc	(revision 8147)
+++ trunk/src/lib/network/network_manager.cc	(revision 8228)
@@ -51,7 +51,5 @@
 
   /* initialize the references */
-  this->netStreamList = NULL;
-  this->syncList = NULL;
-  this->defaultSyncStream = NULL;
+  this->networkStream = NULL;
   this->sharedNetworkData = SharedNetworkData::getInstance();
   this->elapsedTime = 0.0f;
@@ -71,5 +69,11 @@
  */
 NetworkManager::~NetworkManager()
-{}
+{
+  if ( this->networkStream )
+  {
+    delete this->networkStream;
+    this->networkStream = NULL;
+  }
+}
 
 
@@ -79,8 +83,5 @@
 void NetworkManager::initialize()
 {
-  /* get the synchronizeable list from the class list */
-  this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE);
   PRINTF(0)("NetworkManager initzalized\n");
-
 }
 
@@ -101,7 +102,7 @@
 int NetworkManager::establishConnection(const std::string & name, unsigned int port)
 {
-  this->defaultSyncStream = new NetworkStream( name, port );
-  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
-  this->defaultSyncStream->startHandshake();
+  this->networkStream = new NetworkStream( name, port );
+  this->sharedNetworkData->setDefaultSyncStream(this->networkStream);
+  this->networkStream->startHandshake();
   return 1;
 }
@@ -116,7 +117,7 @@
   this->sharedNetworkData->setHostID(0);
   this->sharedNetworkData->setGameServer(true);
-  this->defaultSyncStream = new NetworkStream(port);
-  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
-  this->defaultSyncStream->createNetworkGameManager();
+  this->networkStream = new NetworkStream(port);
+  this->sharedNetworkData->setDefaultSyncStream(this->networkStream);
+  this->networkStream->createNetworkGameManager();
   PRINTF(0)("CREATE SERVER\n");
   SDL_Delay(20);
@@ -127,6 +128,6 @@
 void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
 {
-  if( this->defaultSyncStream)
-    this->defaultSyncStream->connectSynchronizeable(sync);
+  if( this->networkStream)
+    this->networkStream->connectSynchronizeable(sync);
 }
 
@@ -143,11 +144,6 @@
   this->elapsedTime = 0.0f;
 
-  if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL)
-  {
-    std::list<BaseObject*>::const_iterator stream;
-    for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream)
-      if( static_cast<NetworkStream*>(*stream)->isActive())
-        static_cast<NetworkStream*>(*stream)->processData();
-  }
+  if ( networkStream->isActive() )
+    networkStream->processData();
   
   NetworkGameManager::getInstance()->tick( this->elapsedTime );
@@ -162,5 +158,5 @@
 {
   PRINT(0)("=================Network::debug()=========\n");
-  this->defaultSyncStream->debug();
+  this->networkStream->debug();
   PRINT(0)("===========================================\n");
 }
Index: trunk/src/lib/network/network_manager.h
===================================================================
--- trunk/src/lib/network/network_manager.h	(revision 8147)
+++ trunk/src/lib/network/network_manager.h	(revision 8228)
@@ -55,8 +55,6 @@
 
   private:
-    const std::list<BaseObject*>*    netStreamList;           // list with refs to all network streams
-    const std::list<BaseObject*>*    syncList;                // list of synchronizeables
     static NetworkManager*           singletonRef;            //!< Pointer to the only instance of this Class
-    NetworkStream*                   defaultSyncStream;       //!< FIXME: this is only for testing purposes
+    NetworkStream*                   networkStream;       //!< pointer to network stream
 
     SharedNetworkData*               sharedNetworkData;       //!< reference to the shared data
Index: trunk/src/lib/network/network_stream.cc
===================================================================
--- trunk/src/lib/network/network_stream.cc	(revision 8147)
+++ trunk/src/lib/network/network_stream.cc	(revision 8228)
@@ -105,4 +105,5 @@
     serverSocket->close();
     delete serverSocket;
+    serverSocket = NULL;
   }
 
@@ -123,10 +124,6 @@
   }
   
-  if ( serverSocket )
-  {
-    delete serverSocket;
-    serverSocket = NULL;
-  }
-
+  for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ )
+    (*it)->setNetworkStream( NULL );
 }
 
@@ -264,5 +261,5 @@
 
   //check if connections are ok else remove them
-  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
+  for ( PeerList::iterator it = peers.begin(); it != peers.end(); )
   {
     if (  
@@ -297,6 +294,14 @@
 
       freeSocketSlots.push_back( it->second.userId );
-
-    }
+      
+      PeerList::iterator delit = it;
+      it++;
+      
+      peers.erase( delit );
+      
+      continue;
+    }
+    
+    it++;
   }
 
@@ -651,4 +656,5 @@
           }
         }
+        
 
         int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); 
@@ -664,6 +670,4 @@
       }
       
-      //TODO REMOVE THIS
-      int saveOffset = offset;
       
       for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
@@ -682,7 +686,5 @@
       assert( peer->second.lastRecvedState < state );
       peer->second.lastRecvedState = state;
-      
-      assert( saveOffset == offset );
-      
+
     }
   
Index: trunk/src/lib/network/player_stats.cc
===================================================================
--- trunk/src/lib/network/player_stats.cc	(revision 8147)
+++ trunk/src/lib/network/player_stats.cc	(revision 8228)
@@ -21,4 +21,5 @@
 #include "player.h"
 #include "state.h"
+#include "shared_network_data.h"
 
 
@@ -85,5 +86,5 @@
     this->setPlayableUniqueId( this->playableUniqueId );
     
-    PRINTF(0)("uniqueID changed %d %d\n", userId, getHostID());
+    PRINTF(0)("uniqueID changed %d %d %d\n", userId, getHostID(), getUniqueID());
   }
 }
@@ -99,5 +100,7 @@
   
   if ( !list )
+  {
     return NULL;
+  }
   
   for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
@@ -108,6 +111,4 @@
     }
   }
-  
-  //TODO maybe we should create an object here
   
   return NULL;
@@ -133,4 +134,6 @@
     {
       this->playable = dynamic_cast<Playable*>(*it);
+      //TODO when OM_PLAYERS is ticked add line:
+      //this->playable->toList( OM_PLAYERS );
       break;
     }
@@ -138,5 +141,7 @@
   
   if ( this->playable && userId == getHostID() )
+  {
     State::getPlayer()->setPlayable( this->playable );
+  }
   
   this->playableUniqueId = uniqueId;
Index: trunk/src/lib/network/synchronizeable.cc
===================================================================
--- trunk/src/lib/network/synchronizeable.cc	(revision 8147)
+++ trunk/src/lib/network/synchronizeable.cc	(revision 8228)
@@ -369,31 +369,30 @@
 void Synchronizeable::cleanUpUser( int userId )
 {
-  for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ )
-  {
-    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
-    {
-      if ( (*it2)->data )
-        delete [] (*it2)->data;
-      (*it2)->data = NULL;
-      
-      delete *it2;
-    }
-  }
-  
-  sentStates.clear();
-  
-  for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ )
-  {
-    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
-    {
-      if ( (*it2)->data )
-        delete [] (*it2)->data;
-      (*it2)->data = NULL;
-      
-      delete *it2;
-    }
-  }
-  
-  recvStates.clear();
+  if ( recvStates.size() > userId )
+  {
+    for ( std::list<StateHistoryEntry*>::iterator it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
+    {
+      if ( (*it)->data )
+        delete [] (*it)->data;
+      (*it)->data = NULL;
+    
+      delete *it;
+    }
+    recvStates[userId].clear();
+  }
+ 
+  if ( sentStates.size() > userId )
+  {
+    
+    for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
+    {
+      if ( (*it)->data )
+        delete [] (*it)->data;
+      (*it)->data = NULL;
+    
+      delete *it;
+    }
+    sentStates[userId].clear();
+  }
 }
 
Index: trunk/src/lib/network/udp_server_socket.cc
===================================================================
--- trunk/src/lib/network/udp_server_socket.cc	(revision 8147)
+++ trunk/src/lib/network/udp_server_socket.cc	(revision 8228)
@@ -44,5 +44,5 @@
 UdpServerSocket::~UdpServerSocket( )
 {
-  for ( int i = 0; i < packetBuffer.size(); i++ )
+  for ( int i = 0; i < (int)packetBuffer.size(); i++ )
     removeUserPackets( i );
   
@@ -99,5 +99,5 @@
 {
   
-  for ( int i = 0; i < packetBuffer.size(); i++ )
+  for ( int i = 0; i < (int)packetBuffer.size(); i++ )
     removeUserPackets( i );
   
@@ -115,5 +115,5 @@
 void UdpServerSocket::removeUserPackets( int userId )
 {
-  if ( userId >= packetBuffer.size() )
+  if ( userId >= (int)packetBuffer.size() )
     return;
   
@@ -141,9 +141,12 @@
   res.length = 0;
   
-  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
+  if ( (int)packetBuffer.size() > userId && (int)packetBuffer[userId].size() > 0 )
   {
     res.data = packetBuffer[userId].front().data;
     res.length = packetBuffer[userId].front().length;
     packetBuffer[userId].pop_front();
+    
+    if ( res.length == 0 )
+      res.length = -1;
   }
   
@@ -158,5 +161,5 @@
 int UdpServerSocket::getPacketCount( int userId )
 {
-  if ( userId >= packetBuffer.size() )
+  if ( userId >= (int)packetBuffer.size() )
     return -1;
   
@@ -179,11 +182,11 @@
   }
   
-  if ( userId < packetBuffer.size() )
+  if ( userId < (int)packetBuffer.size() )
     removeUserPackets( userId );
   
-  if ( packetBuffer.size() <= userId )
+  if ( (int)packetBuffer.size() <= userId )
     packetBuffer.resize( userId + 1 );
   
-  if ( userList.size() <= userId )
+  if ( (int)userList.size() <= userId )
     userList.resize( userId + 1 );
   
@@ -199,5 +202,5 @@
   removeUserPackets( userId );
   
-  if ( userId >= userList.size() )
+  if ( userId >= (int)userList.size() )
     return;
   
@@ -244,9 +247,9 @@
     bool isNewConnection = false;
     
-    for ( userId =0; userId < userList.size(); userId++ )
+    for ( userId =0; userId < (int)userList.size(); userId++ )
       if ( userList[userId].host == packet->address.host && userList[userId].port == packet->address.port )
         break;
     
-    if ( userId >= userList.size() )
+    if ( userId >= (int)userList.size() )
     {
       
@@ -260,5 +263,5 @@
       }
       
-      for ( userId =0; userId < userList.size(); userId++ )
+      for ( userId =0; userId < (int)userList.size(); userId++ )
         if ( userList[userId].host == 0 && userList[userId].port == 0 )
           break;
@@ -281,7 +284,4 @@
     else
     {
-      if ( isNewConnection )
-        continue;
-      
       networkPacket.data = NULL;
     }
Index: trunk/src/lib/network/udp_socket.cc
===================================================================
--- trunk/src/lib/network/udp_socket.cc	(revision 8147)
+++ trunk/src/lib/network/udp_socket.cc	(revision 8228)
@@ -81,9 +81,9 @@
 UdpSocket::~UdpSocket( )
 {
+  this->disconnectServer();
+  
   if ( serverSocket )
     serverSocket->removeUser( userId );
-  
-  disconnectServer();
-  
+
   if ( this->packet )
     SDLNet_FreePacket( this->packet );
@@ -128,13 +128,13 @@
     return;
   }
-  
+}
+
+/**
+ * disconnect from server
+ */
+void UdpSocket::disconnectServer( )
+{
+  PRINTF(0)("disconnect\n");
   writePacket( NULL, 0 );
-}
-
-/**
- * disconnect from server
- */
-void UdpSocket::disconnectServer( )
-{
   SDLNet_UDP_Unbind( socket, -1 );
   SDLNet_UDP_Close( socket );
@@ -171,5 +171,5 @@
     packet->len = length;
     
-    if ( SDLNet_UDP_Send( socket, 1, packet) == 0 )
+    if ( socket && SDLNet_UDP_Send( socket, 1, packet) == 0 )
     {
       PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
@@ -196,4 +196,10 @@
     NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
     
+    if ( networkPacket.length == -1 )
+    {
+      this->disconnectServer();
+      return 0;
+    }
+    
     if ( networkPacket.length > 0 )
     {
@@ -218,4 +224,10 @@
       assert( packet->len <= maxLength );
       
+      if ( packet->len == 0 )
+      {
+        this->disconnectServer();
+        return 0;
+      }
+      
       memcpy( data, packet->data, packet->len );
       return packet->len;
