Index: branches/network/src/lib/network/netdefs.h
===================================================================
--- branches/network/src/lib/network/netdefs.h	(revision 5619)
+++ branches/network/src/lib/network/netdefs.h	(revision 5624)
@@ -7,8 +7,6 @@
  */
 
-#ifndef _NETWORK_MANAGER
-#define _NETWORK_MANAGER
-
-
+#ifndef _NETDEFS
+#define _NETDEFS
 
 #ifdef HAVE_SDL_NET_H
Index: branches/network/src/lib/network/network_socket.cc
===================================================================
--- branches/network/src/lib/network/network_socket.cc	(revision 5619)
+++ branches/network/src/lib/network/network_socket.cc	(revision 5624)
@@ -37,5 +37,12 @@
   this->setClassID(CL_NETWORK_SOCKET, "NetworkSocket");
 
+  tcpSocket = NULL;
+  bufferlength = 0;
+
+  mutex = SDL_CreateMutex();
+  terminateThread = false;
+
   /* Init SDL_net */
+  //NOTE: do we need to call SDLNet_Init for all instances?
   if(SDLNet_Init()==-1)
   {
@@ -55,6 +62,8 @@
 NetworkSocket::~ NetworkSocket( )
 {
+  SDL_DestroyMutex(mutex);
 
   /* Quit SDL_net */
+  // NOTE: what if other instances of NetworkSocket running?
   SDLNet_Quit();
   PRINTF(5)("SDL_net shutdown\n");
@@ -62,9 +71,16 @@
 
 /**
-  * This function establishes a TCP/UDP connection to a given server (function argument).
-  * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection.
-  */
+ * This function establishes a TCP/UDP connection to a given server (function argument).
+ * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection.
+ * @param ip
+ * @param port
+ */
 void NetworkSocket::connectToServer(IPaddress ip, unsigned int port)
 {
+  //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()!");
+  }
 
   /* Connect to the host and port contained in ip using a TCP connection. */
@@ -76,12 +92,38 @@
   }
 
-}
-
-/**
-  * Tells the NetworkSocket to listen on a specific port for incoming connections.
-  * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
-  */
-void listen(unsigned int port)
-{
+  SDL_CreateThread(thread_read, (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)
+{
+  //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");
+    return;
+  }
+
+  IPaddress ip;
+
+  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
+  {
+    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
+    return;
+  }
+
+  tcpSocket = SDLNet_TCP_Open(&ip);
+
+  if (!tcpSocket)
+  {
+    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+    return;
+  }
+
+  SDL_CreateThread(thread_listen, (void*)this);
 }
 
@@ -91,27 +133,134 @@
 void NetworkSocket::disconnectServer( )
 {
-
+  terminateThread = true;
   /* Close the connection */
   SDLNet_TCP_Close(tcpSocket);
-
-}
-
-/**
-  * This function writes some bytes (data) to the network connection (if the connection is already
-  * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some
-  * warnings
-  */
-int NetworkSocket::writeBytes(byte* data, int length)
-{
-}
-
- /**
-  * Reads in the bytes from the network interface and passes it to the NetworkStream.
-  * This function must internaly be implemented/connected as a thread, since the read
-  * functions of many network libraries are blocking an would therefore block the whole
-  * program.
-  * From outside, the thread shouldn't be accessible at all.
-  */
+  tcpSocket = NULL;
+
+}
+
+
+/**
+ * This function writes some bytes (data) to the network connection (if the connection is already
+ * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some
+ * warnings
+ * @param data: pointer to the data to send
+ * @param length: n bytes to send
+ * @return the number successfully written bytes
+ */
+int NetworkSocket::writeBytes(byte * data, int length)
+{
+  if (!tcpSocket || data==NULL)
+    return 0;
+
+  int res = SDLNet_TCP_Send(tcpSocket, data, length);
+
+  if (res<length)
+  {
+    PRINTF(1)("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+    return res;
+  }
+}
+
+/**
+ * Reads in the bytes from the network interface and passes it to the NetworkStream.
+ * This function must internaly be implemented/connected as a thread, since the read
+ * functions of many network libraries are blocking an would therefore block the whole
+ * program.
+ * From outside, the thread shouldn't be accessible at all.
+ * @param data: pointer to memory, big enough to store length bytes
+ * @param length: n bytes to read
+ * @return the number successfully read bytes. -1 on error. may be less than length!
+ */
 int NetworkSocket::readBytes(byte * data, int length)
 {
-}
+  if (!tcpSocket || data==NULL)
+    return 0;
+
+  int nbytes = (length<bufferlength) ? length : bufferlength;
+
+  // just in case ...
+  if (nbytes<0)
+    return -1;
+
+  if (nbytes==0)
+      return 0;
+
+  SDL_mutexP(mutex);
+
+  memcpy(data, buf, nbytes);
+
+  //important: use memmove because the memory areas may overlap
+  memmove(buf, buf+nbytes, bufferlength-nbytes);
+  bufferlength -= nbytes;
+
+  SDL_mutexV(mutex);
+
+  return nbytes;
+}
+
+/**
+ * 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;
+  TCPsocket tempsocket;
+
+  tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
+
+  SDLNet_TCP_Close(self->tcpSocket);
+  self->tcpSocket = NULL;
+
+  if (!tempsocket)
+  {
+    PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
+    return -1;
+  }
+
+  self->tcpSocket = tempsocket;
+
+  return thread_read(data);
+}
+
+/**
+ * used to create a thread to read from socket
+ * @param data: pointer to NetworkSocket
+ */
+int NetworkSocket::thread_read( void * data )
+{
+  int nbytesread = 0;
+  int nbytestoread = 0;
+  char buffer[_LOCAL_BUFFER_SIZE];
+  NetworkSocket * self = (NetworkSocket*)data;
+
+  while (!self->terminateThread)
+  {
+#define min(a,b) (a<b)?a:b
+    nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE);
+
+    nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread);
+
+    SDL_mutexP(self->mutex);
+
+    if (nbytesread<=0)
+    {
+      PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
+      SDLNet_TCP_Close(self->tcpSocket);
+      self->tcpSocket = NULL;
+      SDL_mutexV(self->mutex);
+      return -1;
+    }
+
+    memcpy(self->buf+self->bufferlength, buffer, nbytesread);
+    self->bufferlength += nbytesread;
+
+    SDL_mutexV(self->mutex);
+  }
+
+  return 0;
+}
+
+
Index: branches/network/src/lib/network/network_socket.h
===================================================================
--- branches/network/src/lib/network/network_socket.h	(revision 5619)
+++ branches/network/src/lib/network/network_socket.h	(revision 5624)
@@ -7,4 +7,12 @@
 #ifndef _NETWORK_SOCKET
 #define _NETWORK_SOCKET
+
+#define _INCOMING_BUFFER_SIZE 10240
+#define _LOCAL_BUFFER_SIZE 1024
+
+/* contains memmove and memcpy */
+#include <string.h>
+
+#include <SDL_thread.h>
 
 /* include this file, it contains some default definitions */
@@ -22,8 +30,17 @@
 
 private:
-  IPaddress serverAddress;
-  unsigned int port;
+//  IPaddress serverAddress;
+//  unsigned int port;
   TCPsocket tcpSocket;
-  UDPsocket udpSocket;
+//  UDPsocket udpSocket;
+
+  byte buf[_INCOMING_BUFFER_SIZE];
+  int bufferlength;
+
+  SDL_mutex * mutex;
+  bool terminateThread;
+
+  static int thread_listen(void * data);
+  static int thread_read(void * data);
 
 public:
