Index: branches/network/src/lib/network/network_socket.cc
===================================================================
--- branches/network/src/lib/network/network_socket.cc	(revision 5628)
+++ branches/network/src/lib/network/network_socket.cc	(revision 5630)
@@ -38,8 +38,10 @@
 
   tcpSocket = NULL;
-  bufferlength = 0;
-
-  mutex = SDL_CreateMutex();
-  socketmutex = SDL_CreateMutex();
+  incomingBufferLength = 0;
+  outgoingBufferLength = 0;
+
+  incomingBufferMutex = SDL_CreateMutex();
+  outgoingBufferMutex = SDL_CreateMutex();
+  socketMutex = SDL_CreateMutex();
   terminateThread = false;
 
@@ -68,6 +70,9 @@
   PRINTF(5)("SDL_net shutdown\n");
 
-  SDL_DestroyMutex(mutex);
-  SDL_DestroyMutex(socketmutex);
+  _isListening = false;
+
+  SDL_DestroyMutex(incomingBufferMutex);
+  SDL_DestroyMutex(outgoingBufferMutex);
+  SDL_DestroyMutex(socketMutex);
 }
 
@@ -94,5 +99,8 @@
   }
 
+  _isListening = false;
+
   SDL_CreateThread(thread_read, (void*)this);
+  SDL_CreateThread(thread_write, (void*)this);
 }
 
@@ -104,8 +112,10 @@
 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;
   }
@@ -116,4 +126,5 @@
   {
     PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
+    _isListening = false;
     return;
   }
@@ -124,8 +135,10 @@
   {
     PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+    _isListening = false;
     return;
   }
 
   SDL_CreateThread(thread_listen, (void*)this);
+  SDL_CreateThread(thread_write, (void*)this);
 }
 
@@ -138,8 +151,8 @@
   /* Close the connection */
 
-  SDL_mutexP(socketmutex);
+  SDL_mutexP(socketMutex);
   SDLNet_TCP_Close(tcpSocket);
   tcpSocket = NULL;
-  SDL_mutexV(socketmutex);
+  SDL_mutexV(socketMutex);
 
 }
@@ -156,5 +169,49 @@
 int NetworkSocket::writeBytes(byte * data, int length)
 {
-  SDL_mutexP(socketmutex);
+#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
+  int nbytes = min(_OUTGOING_BUFFER_SIZE - outgoingBufferLength, length);
+#undef min
+
+  if (!tcpSocket || data==NULL || nbytes<=0)
+    return 0;
+
+  SDL_mutexP(outgoingBufferMutex);
+
+  memcpy(outgoingBuffer + outgoingBufferLength, data, nbytes);
+  outgoingBufferLength += nbytes;
+
+  SDL_mutexV(outgoingBufferMutex);
+
+  return nbytes;
+#else
+  SDL_mutexP(socketMutex);
 
   if (!tcpSocket || data==NULL)
@@ -163,5 +220,5 @@
   int res = SDLNet_TCP_Send(tcpSocket, data, length);
 
-  SDL_mutexV(socketmutex);
+  SDL_mutexV(socketMutex);
 
   if (res<length)
@@ -169,4 +226,5 @@
 
   return res;
+#endif
 }
 
@@ -186,5 +244,7 @@
     return 0;
 
-  int nbytes = (length<bufferlength) ? length : bufferlength;
+  int nbytes = (length<incomingBufferLength) ? length : incomingBufferLength;
+
+  //printf("readBytes: nbytes = %d; length=%d; incomingBufferLength=%d\n", nbytes, length, incomingBufferLength);
 
   // just in case ...
@@ -195,13 +255,13 @@
       return 0;
 
-  SDL_mutexP(mutex);
-
-  memcpy(data, buf, nbytes);
+  SDL_mutexP(incomingBufferMutex);
+
+  memcpy(data, incomingBuffer, nbytes);
 
   //important: use memmove because the memory areas may overlap
-  memmove(buf, buf+nbytes, bufferlength-nbytes);
-  bufferlength -= nbytes;
-
-  SDL_mutexV(mutex);
+  memmove(incomingBuffer, incomingBuffer+nbytes, incomingBufferLength-nbytes);
+  incomingBufferLength -= nbytes;
+
+  SDL_mutexV(incomingBufferMutex);
 
   return nbytes;
@@ -216,9 +276,13 @@
 {
   NetworkSocket * self = (NetworkSocket*)data;
+  self->_isListening = true;
   TCPsocket tempsocket;
 
   tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
 
-  SDL_mutexP(self->socketmutex);
+  while (!tempsocket && !self->terminateThread)
+    tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
+
+  SDL_mutexP(self->socketMutex);
   SDLNet_TCP_Close(self->tcpSocket);
   self->tcpSocket = NULL;
@@ -227,5 +291,7 @@
   {
     PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
-    SDL_mutexV(self->socketmutex);
+    //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
+    SDL_mutexV(self->socketMutex);
+    self->_isListening = false;
     return -1;
   }
@@ -233,6 +299,7 @@
   self->tcpSocket = tempsocket;
 
-  SDL_mutexV(self->socketmutex);
-
+  SDL_mutexV(self->socketMutex);
+
+  self->_isListening = false;
   return thread_read(data);
 }
@@ -252,8 +319,9 @@
   {
 #define min(a,b) (a<b)?a:b
-    nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE);
+    nbytestoread = min(_INCOMING_BUFFER_SIZE - self->incomingBufferLength, _LOCAL_BUFFER_SIZE);
+#undef min
 
     //if buffer is full
-    if (nbytestoread<=0)
+    if (nbytestoread<=0 || self->_isListening)
     {
       SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
@@ -263,5 +331,5 @@
     nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread);
 
-    SDL_mutexP(self->mutex);
+    SDL_mutexP(self->incomingBufferMutex);
 
     if (nbytesread<=0)
@@ -269,18 +337,20 @@
       PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
 
-      SDL_mutexP(self->socketmutex);
+      SDL_mutexP(self->socketMutex);
 
       SDLNet_TCP_Close(self->tcpSocket);
       self->tcpSocket = NULL;
 
-      SDL_mutexV(self->socketmutex);
-      SDL_mutexV(self->mutex);
+      SDL_mutexV(self->socketMutex);
+      SDL_mutexV(self->incomingBufferMutex);
       return -1;
     }
 
-    memcpy(self->buf+self->bufferlength, buffer, nbytesread);
-    self->bufferlength += nbytesread;
-
-    SDL_mutexV(self->mutex);
+    //printf("thread_read: nbytesread=%d\n", nbytesread);
+
+    memcpy(self->incomingBuffer+self->incomingBufferLength, buffer, nbytesread);
+    self->incomingBufferLength += nbytesread;
+
+    SDL_mutexV(self->incomingBufferMutex);
   }
 
@@ -288,4 +358,54 @@
 }
 
-
-
+int NetworkSocket::thread_write( void * data )
+{
+  int nbyteswrite = 0;
+  int nbytestowrite = 0;
+  char buffer[_LOCAL_BUFFER_SIZE];
+  NetworkSocket * self = (NetworkSocket*)data;
+
+  while (!self->terminateThread)
+  {
+#define min(a,b) (a<b)?a:b
+    nbytestowrite = min(self->outgoingBufferLength, _LOCAL_BUFFER_SIZE);
+#undef min
+
+    //printf("thread_write nbytes=%d listening=%d\n", nbytestowrite, (int)self->_isListening);
+
+    //if buffer is full
+    if (nbytestowrite<=0 || self->_isListening)
+    {
+      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
+      continue;
+    }
+
+    SDL_mutexP(self->outgoingBufferMutex);
+
+    //printf("a\n");
+
+    memcpy(buffer, self->outgoingBuffer, nbytestowrite);
+    self->outgoingBufferLength -= nbytestowrite;
+    memmove(self->outgoingBuffer, self->outgoingBuffer+nbytestowrite, self->outgoingBufferLength);
+
+    SDL_mutexV(self->outgoingBufferMutex);
+
+    nbyteswrite = SDLNet_TCP_Send(self->tcpSocket, buffer, nbytestowrite);
+
+    if (nbyteswrite<=0)
+    {
+      PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
+
+      SDL_mutexP(self->socketMutex);
+
+      SDLNet_TCP_Close(self->tcpSocket);
+      self->tcpSocket = NULL;
+
+      SDL_mutexV(self->socketMutex);
+      return -1;
+    }
+
+  }
+
+  return 0;
+}
+
Index: branches/network/src/lib/network/network_socket.h
===================================================================
--- branches/network/src/lib/network/network_socket.h	(revision 5628)
+++ branches/network/src/lib/network/network_socket.h	(revision 5630)
@@ -8,7 +8,14 @@
 #define _NETWORK_SOCKET
 
+//if you want to use outgoing buffer define _USE_OUTGOING_BUFFER
+#define _USE_OUTGOING_BUFFER
+
 #define _INCOMING_BUFFER_SIZE 10240
+#define _OUTGOING_BUFFER_SIZE 302400
 #define _LOCAL_BUFFER_SIZE 1024
+//sleep if incoming buffer is full
 #define _MSECONDS_SLEEP_FULL_BUFFER 10
+//sleep if outgoing buffer is empty
+#define _MSECONDS_SLEEP_EMPTY_BUFFER 10
 
 /* contains memmove and memcpy */
@@ -36,13 +43,27 @@
 //  UDPsocket udpSocket;
 
-  byte buf[_INCOMING_BUFFER_SIZE];
-  int bufferlength;
+  byte incomingBuffer[_INCOMING_BUFFER_SIZE];
+#ifdef _USE_OUTGOING_BUFFER
+  byte outgoingBuffer[_OUTGOING_BUFFER_SIZE];
+#endif
+  int incomingBufferLength;
+#ifdef _USE_OUTGOING_BUFFER
+  int outgoingBufferLength;
+#endif
 
-  SDL_mutex * mutex;
-  SDL_mutex * socketmutex;
+  SDL_mutex * incomingBufferMutex;
+#ifdef _USE_OUTGOING_BUFFER
+  SDL_mutex * outgoingBufferMutex;
+#endif
+  SDL_mutex * socketMutex;
   bool terminateThread;
 
   static int thread_listen(void * data);
   static int thread_read(void * data);
+#ifdef _USE_OUTGOING_BUFFER
+  static int thread_write(void * data);
+#endif
+
+  bool _isListening;
 
 public:
Index: branches/network/src/subprojects/network/network_unit_test.cc
===================================================================
--- branches/network/src/subprojects/network/network_unit_test.cc	(revision 5628)
+++ branches/network/src/subprojects/network/network_unit_test.cc	(revision 5630)
@@ -27,4 +27,5 @@
   SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
   server.listen(9999);
+  SDL_Delay(20);
   client.connectToServer(ip, 9999);
   char buf[1024];
@@ -37,12 +38,12 @@
 
   int n;
-	char * str1 = "client to server";
+  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);
+  printf("%d bytes send from client\n", n);
   n = server.writeBytes((byte*)str2, strlen(str2)+1);
-	printf("%d bytes send from server\n", n);
+  printf("%d bytes send from server\n", n);
   SDL_Delay(1000);
-  
+
   printf("read from server\n");
   n = server.readBytes((byte*)buf, 1024);
@@ -63,15 +64,17 @@
   printf("testing a bigger amount of data\n");
 
-#define _N_ELEMENTS 212992
-  char sendbuf[_N_ELEMENTS];
-  char recvbuf[_N_ELEMENTS];
+#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%30 + 30;
+    sendbuf[i] = i%26 + 65;
 
   printf("write\n");
-  client.writeBytes((byte*)sendbuf, _N_ELEMENTS);
+  printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS));
 
-  SDL_Delay(500);
+  SDL_Delay(50);
 
   printf("read\n");
@@ -82,11 +85,22 @@
   {
     SDL_Delay(10);
-    printf("read\n");
+    //printf("read\n");
     nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset);
     offset += nbytes;
-    printf("nbytes=%d, offset=%d\n", nbytes, offset);
+    //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;
+    }
+  }
 
   return 0;
