Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8864)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8865)
@@ -12,4 +12,5 @@
 
 #include "MString.h"
+#include "MTimeout.h"
 
 #undef DEBUG
@@ -84,20 +85,20 @@
     }
 
-    const Int_t l = tx.SendRaw(msg, len);
-    if (l<0)
-    {
-        gLog << err << "ERROR - Sending TCP/IP frame to " << GetSocketAddress(tx) << endl;
-        return false;
-    }
-
-    if (l!=len)
-    {
-        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
-        return false;
-    }
-
 #ifdef DEBUG
     cout << "Tx: " << msg << flush;
 #endif
+
+    const Int_t l = tx.SendRaw(msg, len);
+    if (l<0)
+    {
+        gLog << err << "ERROR - Sending TCP/IP frame to " << GetSocketAddress(tx) << endl;
+        return false;
+    }
+
+    if (l!=len)
+    {
+        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
+        return false;
+    }
 
     return true;
@@ -164,14 +165,11 @@
         const Int_t len = rx.RecvRaw(&c, 1);
 
-        // No data received (non-blocking mode)
-        if (len<0)
-        {
-            usleep(1);
-            continue;
-        }
-
-        // Data received with zero length! (Connection lost)
-        if (len==0)
-            return kFALSE; // This break is for TEST PURPOSE FIXME!!!
+        // For details see TSocket::RecvRaw
+        // -1: // ERROR
+        // EINVAL, EWOULDBLOCK
+        // -5: // EPIPE || ECONNRESET = Pipe broken or connection reset by peer
+        //  0: Data received with zero length! (Connection lost/call interrupted)
+        if (len<=0)
+            return kFALSE;
 
         // Data received
@@ -208,4 +206,6 @@
     fConnectionEstablished = kTRUE;
 
+    MTimeout timeout;
+
     // Get connection on port fPortRx and redirected
     while (!IsThreadCanceled())
@@ -220,7 +220,15 @@
                 return kFALSE;
             }
-
-            // *FALLTHROUGH*
+            timeout.Start(fTimeout);
+
+            // Go on waiting for new data
+            continue;
+
         case kFALSE: // Time out, no data yet, go on waiting
+            if (timeout.HasTimedOut())
+            {
+                gLog << warn << "WARNING - Connection to " << MTcpIpO::GetSocketAddress(sock) << " timed out." << endl;
+                return kFALSE;
+            }
 
             // Go on waiting for new data
@@ -248,4 +256,5 @@
         while (!IsThreadCanceled())
         {
+            // Wait for a new connection on RX port
             socket = server.Accept();
 
@@ -271,4 +280,6 @@
         if ((Long_t)socket<=0)
             return;
+
+        gLog << all << "Connection established to " << MTcpIpO::GetSocketAddress(*socket) << "..." << endl;
 
         if (!WaitForData(*socket))
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 8864)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 8865)
@@ -18,5 +18,7 @@
 {
 private:
-    Int_t  fPortRx;
+    Int_t  fPortRx;     // Port on which to listen for connections
+    Int_t  fTimeout;    // [ms] Timeout to listen for data to be received
+
     Bool_t fConnectionEstablished;
 
@@ -29,5 +31,5 @@
 
 public:
-    MTcpIpI(Int_t rx) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
+    MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
     ~MTcpIpI() { CancelThread(); }
 
