Index: branches/network/src/defs/class_id.h
===================================================================
--- branches/network/src/defs/class_id.h	(revision 5986)
+++ branches/network/src/defs/class_id.h	(revision 5987)
@@ -224,5 +224,6 @@
   CL_NETWORK_PROTOCOL           =    0x00000b02,
   CL_NETWORK_SOCKET             =    0x00000b03,
-  CL_CONNECTION_MONITOR         =    0x00000b04,
+  CL_SERVER_SOCKET              =    0X00000b04,
+  CL_CONNECTION_MONITOR         =    0x00000b05,
 
 
Index: branches/network/src/lib/network/Makefile.am
===================================================================
--- branches/network/src/lib/network/Makefile.am	(revision 5986)
+++ branches/network/src/lib/network/Makefile.am	(revision 5987)
@@ -10,5 +10,6 @@
 		      network_stream.cc \
                       data_stream.cc \
-                      network_protocol.cc
+                      network_protocol.cc \
+		      server_socket.cc
 
 
@@ -20,4 +21,5 @@
 		 network_stream.h \
                  data_stream.h \
-                 network_protocol.h
+                 network_protocol.h \
+		 server_socket.h
 
Index: branches/network/src/lib/network/network_socket.cc
===================================================================
--- branches/network/src/lib/network/network_socket.cc	(revision 5986)
+++ branches/network/src/lib/network/network_socket.cc	(revision 5987)
@@ -47,4 +47,13 @@
 
 
+NetworkSocket::NetworkSocket( TCPsocket sock )
+{
+  this->init();
+  this->tcpSocket = sock;
+
+  SDL_CreateThread(thread_read, (void*)this);
+  SDL_CreateThread(thread_write, (void*)this);
+}
+
 void NetworkSocket::init()
 {
@@ -75,4 +84,6 @@
 }
 
+
+
 /**
  * Default destructor
@@ -85,6 +96,4 @@
   PRINTF(5)("SDL_net shutdown\n");
 
-  _isListening = false;
-
   SDL_DestroyMutex(incomingBufferMutex);
   SDL_DestroyMutex(outgoingBufferMutex);
@@ -113,47 +122,8 @@
   }
 
-  _isListening = false;
-
   SDL_CreateThread(thread_read, (void*)this);
   SDL_CreateThread(thread_write, (void*)this);
 }
 
-/**
- * Tells the NetworkSocket to listen on a specific port for incoming connections.
- * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
- * @param port
- */
-void NetworkSocket::listen(unsigned int port)
-{
-  _isListening = true;
-  //check if not already connected or listening
-  if (tcpSocket)
-  {
-    PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");
-    _isListening = false;
-    return;
-  }
-
-  IPaddress ip;
-
-  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
-  {
-    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
-    _isListening = false;
-    return;
-  }
-
-  tcpSocket = SDLNet_TCP_Open(&ip);
-
-  if (!tcpSocket)
-  {
-    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
-    _isListening = false;
-    return;
-  }
-
-  SDL_CreateThread(thread_listen, (void*)this);
-  SDL_CreateThread(thread_write, (void*)this);
-}
 
 /**
@@ -184,30 +154,4 @@
   PRINTF(0)("NetworkSocket::writeBytes()\n");
 #ifdef _USE_OUTGOING_BUFFER
-
-  //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);
-//   if (length>_OUTGOING_BUFFER_SIZE)
-//   {
-//     int res = 0;
-//     int n = length / _OUTGOING_BUFFER_SIZE;
-//     if (length % _OUTGOING_BUFFER_SIZE != 0)
-//       n++;
-// //     printf("n=%d\n", n);
-//     SDL_Delay(500);
-//     for (int i = 0; i<n; i++)
-//     {
-// //       printf("i=%d\n", i);
-//       if (i==n-1)
-//       {
-//         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);
-// //         printf("res = %d\n", res);
-//       }
-//       else
-//       {
-//         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);
-// //         printf("res = %d\n", res);
-//       }
-//     }
-//     return res;
-//   }
 
 #define min(a,b) (a<b)?a:b
@@ -256,4 +200,5 @@
 int NetworkSocket::readBytes(byte * data, int length)
 {
+  PRINTF(0)("NetworkSocket::readBytes()\n");
   if (data==NULL)
     return 0;
@@ -299,43 +244,4 @@
 }
 
-/**
- * used to create a thread to listen
- * will call thrad_read when established connection
- * @param data: pointer to NetwortSocket
- */
-int NetworkSocket::thread_listen( void * data )
-{
-  NetworkSocket * self = (NetworkSocket*)data;
-  self->_isListening = true;
-  TCPsocket tempsocket;
-
-  tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
-
-  while (!tempsocket && !self->terminateThread)
-  {
-    tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
-    SDL_Delay(_MSECONDS_SLEEP_LISTEN);
-  }
-
-  SDL_mutexP(self->socketMutex);
-  SDLNet_TCP_Close(self->tcpSocket);
-  self->tcpSocket = NULL;
-
-  if (!tempsocket)
-  {
-    printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
-    //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
-    SDL_mutexV(self->socketMutex);
-    self->_isListening = false;
-    return -1;
-  }
-
-  self->tcpSocket = tempsocket;
-
-  SDL_mutexV(self->socketMutex);
-
-  self->_isListening = false;
-  return thread_read(data);
-}
 
 /**
@@ -357,5 +263,5 @@
 
     //if buffer is full
-    if (nbytestoread<=0 || self->_isListening)
+    if (nbytestoread<=0)
     {
       SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
@@ -408,5 +314,5 @@
 
     //if buffer is full
-    if (nbytestowrite<=0 || self->_isListening)
+    if (nbytestowrite<=0)
     {
       SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
@@ -445,2 +351,49 @@
 }
 
+bool NetworkSocket::writePacket( byte * data, int length )
+{
+  PRINTF(0)("NetworkSocket::writePacket()\n");
+  if (length>255)
+  {
+    PRINTF(1)("Packet length > 255!\n");
+    return false;
+  }
+
+  byte blen = length;
+
+  writeBytes(&blen, 1);
+  writeBytes(data, length);
+}
+
+int NetworkSocket::readPacket( byte * data, int maxLength )
+{
+  PRINTF(0)("NetworkSocket::readPacket()\n");
+  if (incomingBufferLength<1)
+  {
+    return 0;
+  }
+
+  byte blen = incomingBuffer[0];
+
+  if (blen>maxLength)
+  {
+    PRINTF(1)("Buffersize is too small (%d) for packet (%d)\n", maxLength, blen);
+    return 0;
+  }
+
+  if (blen>incomingBufferLength)
+  {
+    return 0;
+  }
+
+  readBytes(&blen, 1);
+  int res = readBytes(data, blen);
+
+  if (res!=blen)
+    return -1;
+  else
+    return blen;
+
+}
+
+
Index: branches/network/src/lib/network/network_socket.h
===================================================================
--- branches/network/src/lib/network/network_socket.h	(revision 5986)
+++ branches/network/src/lib/network/network_socket.h	(revision 5987)
@@ -18,6 +18,5 @@
 //sleep if outgoing buffer is empty
 #define _MSECONDS_SLEEP_EMPTY_BUFFER 10
-//sleep when waiting for connections
-#define _MSECONDS_SLEEP_LISTEN 100
+
 
 /* contains memmove and memcpy */
@@ -64,5 +63,4 @@
   bool terminateThread;
 
-  static int thread_listen(void * data);
   static int thread_read(void * data);
 #ifdef _USE_OUTGOING_BUFFER
@@ -70,5 +68,9 @@
 #endif
 
-  bool _isListening;
+  int writeBytes(byte * data, int length);
+  int readBytes(byte * data, int length);
+  int readBlock(byte * data, int length);
+
+  void init();
 
 public:
@@ -76,15 +78,12 @@
   NetworkSocket();
   NetworkSocket(IPaddress ip);
+  NetworkSocket(TCPsocket sock);
   ~NetworkSocket();
 
   void connectToServer(IPaddress ip);
-  void listen(unsigned int port);
   void disconnectServer();
-  int writeBytes(byte * data, int length);
-  int readBytes(byte * data, int length);
-  int readBlock(byte * data, int length);
 
-private:
-  void init();
+  bool writePacket(byte * data, int length);
+  int readPacket(byte * data, int maxLength);
 
 };
Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 5986)
+++ branches/network/src/lib/network/network_stream.cc	(revision 5987)
@@ -67,5 +67,5 @@
   this->init();
   this->networkSocket = new NetworkSocket();
-  this->networkSocket->listen(port);
+//  this->networkSocket->listen(port);
   this->networkProtocol = new NetworkProtocol();
   this->synchronizeables = NULL;
@@ -93,5 +93,5 @@
   this->init();
   this->networkSocket = new NetworkSocket();
-  this->networkSocket->listen(port);
+//  this->networkSocket->listen(port);
   this->networkProtocol = new NetworkProtocol();
   this->synchronizeables = &sync;
@@ -151,5 +151,5 @@
 
     /* pass the data to the network socket */
-    dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
+ //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
     /* check if there was an error */
     if( dataLength == -1)
@@ -167,5 +167,5 @@
   if( this->state == NET_REC_HEADER)
   {
-    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
+//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
     if( dataLength == sizeof(Header))
     {
@@ -182,5 +182,5 @@
   {
     /* now read the data */
-    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
+//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
     /* check if the data is available and process it if so */
     if( dataLength == this->packetHeader.length)
Index: branches/network/src/lib/network/server_socket.cc
===================================================================
--- branches/network/src/lib/network/server_socket.cc	(revision 5987)
+++ branches/network/src/lib/network/server_socket.cc	(revision 5987)
@@ -0,0 +1,125 @@
+/*
+   orxonox - the future of 3D-vertical-scrollers
+
+   Copyright (C) 2004 orx
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+### File Specific:
+   main-programmer: Christoph Renner
+   co-programmer:
+*/
+
+/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
+   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
+*/
+#define DEBUG_MODULE_NETWORK
+
+
+/* include your own header */
+#include "server_socket.h"
+
+/* header for debug output */
+#include "debug.h"
+
+ServerSocket::ServerSocket(  )
+{
+  init();
+}
+
+ServerSocket::ServerSocket( unsigned int port )
+{
+  init();
+  listen(port);
+}
+
+/**
+ * Default destructor
+ */
+ServerSocket::~ServerSocket( )
+{
+  /* Quit SDL_net */
+  // NOTE: what if other instances of NetworkSocket running?
+  SDLNet_Quit();
+  PRINTF(5)("SDL_net shutdown\n");
+
+  _isListening = false;
+}
+
+void ServerSocket::init( )
+{
+  /* set the class id for the base object */
+  this->setClassID(CL_SERVER_SOCKET, "ServerSocket");
+
+  terminateThread = false;
+  listenSocket = NULL;
+  _isListening = false;
+
+  if(SDLNet_Init()==-1)
+  {
+    PRINTF(1)("SDLNet_Init: %s\n", SDLNet_GetError());
+    return;
+  }
+  else
+    PRINTF(5)("SDL_net initialized\n");
+
+  PRINTF(0)("ServerSocket created\n");
+}
+
+
+/**
+ * Tells the NetworkSocket to listen on a specific port for incoming connections.
+ * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
+ * @param port
+ */
+bool ServerSocket::listen(unsigned int port)
+{
+  PRINTF(0)("ServerSocket::listen()\n");
+  _isListening = true;
+  //check if not already connected or listening
+  if (listenSocket)
+  {
+    PRINTF(1)("ServerSocket::listen: tcpSocket!=NULL! maybe you already called listen or did not call close()!\n");
+    _isListening = false;
+    return false;
+  }
+
+  IPaddress ip;
+
+  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
+  {
+    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
+    _isListening = false;
+    return false;
+  }
+
+  listenSocket = SDLNet_TCP_Open(&ip);
+
+  if (!listenSocket)
+  {
+    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+    _isListening = false;
+    return false;
+  }
+
+  return true;
+}
+
+
+NetworkSocket ServerSocket::getNewSocket( )
+{
+  TCPsocket sock = SDLNet_TCP_Accept(listenSocket);
+
+  if ( !sock )
+  {
+    return NULL;
+  }
+  else
+  {
+    return NetworkSocket(sock);
+  }
+}
+
Index: branches/network/src/lib/network/server_socket.h
===================================================================
--- branches/network/src/lib/network/server_socket.h	(revision 5987)
+++ branches/network/src/lib/network/server_socket.h	(revision 5987)
@@ -0,0 +1,44 @@
+/*!
+ * @file server_socket.h
+ *  waits for incoming connections
+
+ */
+
+#ifndef _SERVER_SOCKET
+#define _SERVER_SOCKET
+
+#ifdef HAVE_SDL_H
+#include <SDL_thread.h>
+#else
+#include <SDL/SDL_thread.h>
+#endif
+/* include this file, it contains some default definitions */
+#include "netdefs.h"
+
+
+/* include base_object.h since all classes are derived from this one */
+#include "base_object.h"
+#include "network_socket.h"
+
+//sleep when waiting for connections
+#define _MSECONDS_SLEEP_LISTEN 100
+
+class ServerSocket : public BaseObject
+{
+  private:
+    TCPsocket listenSocket;
+    bool terminateThread;
+
+    bool _isListening;
+
+    void init();
+
+  public:
+    ServerSocket();
+    ServerSocket( unsigned int port );
+    ~ServerSocket();
+    bool listen( unsigned int port );
+    NetworkSocket getNewSocket( void );
+};
+
+#endif
Index: branches/network/src/subprojects/network/network_unit_test.cc
===================================================================
--- branches/network/src/subprojects/network/network_unit_test.cc	(revision 5986)
+++ branches/network/src/subprojects/network/network_unit_test.cc	(revision 5987)
@@ -8,4 +8,5 @@
 #include "network_manager.h"
 #include "network_socket.h"
+#include "server_socket.h"
 #include "network_stream.h"
 #include "synchronizeable.h"
@@ -36,85 +37,79 @@
   IPaddress ip;
   SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
-  NetworkSocket server;
+  ServerSocket server;
   server.listen(9999);
-  SDL_Delay(20);
-
-  NetworkSocket client(ip);
+
+  NetworkSocket client1(ip);
+
+  NetworkSocket server1 = server.getNewSocket();
+
+  NetworkSocket client2(ip);
+
+  NetworkSocket server2 = server.getNewSocket();
 
   char buf[1024];
 
-  printf("read from client before sending data\n");
-  printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024));
-
-  printf("read from server before sending data\n");
-  printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024));
+  printf("read from client1 before sending data\n");
+  printf("result: %d bytes\n", client1.readPacket((byte*)buf, 1024));
+
+  printf("read from client2 before sending data\n");
+  printf("result: %d bytes\n", client2.readPacket((byte*)buf, 1024));
 
   int n;
-  char * str1 = "client to server";
-  char * str2 = "server to client";
-  n = client.writeBytes((byte*)str1, strlen(str1)+1);
-  printf("%d bytes send from client\n", n);
-  n = server.writeBytes((byte*)str2, strlen(str2)+1);
-  printf("%d bytes send from server\n", n);
+  char * str1 = "client1 to server";
+  char * str2 = "server1 to client";
+  char * str3 = "client2 to server";
+  char * str4 = "server2 to client";
+  n = client1.writePacket((byte*)str1, strlen(str1)+1);
+  printf("%d bytes send from client1\n", n);
+  n = server1.writePacket((byte*)str2, strlen(str2)+1);
+  printf("%d bytes send from server1\n", n);
+  n = client2.writePacket((byte*)str3, strlen(str3)+1);
+  printf("%d bytes send from client2\n", n);
+  n = server2.writePacket((byte*)str4, strlen(str4)+1);
+  printf("%d bytes send from server2\n", n);
   SDL_Delay(1000);
 
-  printf("read from server\n");
-  n = server.readBytes((byte*)buf, 1024);
+  printf("read from server1\n");
+  n = server1.readPacket((byte*)buf, 1024);
   printf("read %d bytes\n", n);
   if (n<0)
     return -1;
 
-  printf("data: %s\n", buf);
-  printf("read from client\n");
-  n = client.readBytes((byte*)buf, 1024);
-
+  printf("data: '%s'\n", buf);
+
+  printf("read from server2\n");
+  n = server2.readPacket((byte*)buf, 1024);
   printf("read %d bytes\n", n);
   if (n<0)
     return -1;
 
-  printf("data: %s\n", buf);
-
-  printf("testing a bigger amount of data\n");
-
-#define _N_ELEMENTS 212994
-
-  char sendbuf[_N_ELEMENTS+1];
-  char recvbuf[_N_ELEMENTS+1];
-  sendbuf[_N_ELEMENTS] = '\0';
-  recvbuf[_N_ELEMENTS] = '\0';
-
-  for (int i = 0; i<_N_ELEMENTS; i++)
-    sendbuf[i] = i%26 + 65;
-
-  printf("write\n");
-  printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS));
-
-  SDL_Delay(50);
-
-  printf("read\n");
-  int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS);
-  int offset = nbytes;
-
-  while (nbytes>0)
-  {
-    SDL_Delay(10);
-    //printf("read\n");
-    nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset);
-    offset += nbytes;
-    //printf("nbytes=%d, offset=%d\n", nbytes, offset);
-  }
-
-  printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset);
-
-  //printf("%s\n%s\n", sendbuf, recvbuf);
-
-  for (int i = 0; i<_N_ELEMENTS; i++)
-  {
-    if (sendbuf[i]!=recvbuf[i])
-    {
-      printf("byte %d is the first difference\n", i+1);
-      break;
-    }
-  }
+  printf("data: '%s'\n", buf);
+
+  printf("read from client1\n");
+  n = client1.readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+
+  printf("read from client2\n");
+  n = client2.readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+
+  //sending bigger packets than 255 is not supported
+
+  printf("try to send more than 255 bytes\n");
+  printf("result: %d\n", client1.writePacket((byte*)buf, 1000));
+
+  server1.writePacket((byte*)str1, strlen(str1)+1);
+  SDL_Delay(500);
+  printf("try to read with a too small buffer\n");
+  printf("result: %d\n", client1.readPacket((byte*)buf, strlen(str1)));
 
   return 0;
