Index: trunk/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/Cosy/tcpip/MTcpIpIO.cc	(revision 10026)
+++ trunk/Cosy/tcpip/MTcpIpIO.cc	(revision 10031)
@@ -67,8 +67,4 @@
 }
 
-MTcpIpIO::~MTcpIpIO()
-{
-}
-
 TString MTcpIpO::GetSocketAddress(const TSocket &s)
 {
@@ -192,5 +188,87 @@
 
         MThread::Sleep(wait); // Wait a ms
-        if (wait<30000000) // 30s
+        if (wait<3000000) // 3s
+            wait *= 2;
+    }
+
+    return 1;
+}
+
+Int_t MTcpIpOI::Thread()
+{
+    fConnectionEstablished = kFALSE;
+
+    const TInetAddress &a = fTxSocket->GetInetAddress();
+    if (!a.IsValid())
+    {
+        gLog << err << "- MTcpIpOI::Thread - ERROR: Send socket address invalid." << endl;
+        return 0;
+    }
+
+    gLog << inf << "- MTcpIpOI::Thread created connecting to " << a.GetHostAddress() << ":" << fPortTx << endl;
+
+    Int_t wait = 1000; /// Wait a ms
+
+    MTimeout timeout(fTimeout);
+
+    while (!IsThreadCanceled())
+    {
+        if (fTxSocket->IsValid())
+        {
+            fConnectionEstablished = kTRUE;
+            fTimeout = 1000;
+
+            // Get connection on port fPortRx and redirected
+            // Check for pending data (every ms)
+            switch (fTxSocket->Select(TSocket::kRead, 1))
+            {
+            case kTRUE:  // Data pending... go on reading
+                if (!ReadSocket(*fTxSocket))
+                {
+                    gLog << warn << MTime(-1) << " WARNING - Connection lost to " << MTcpIpO::GetSocketAddress(*fTxSocket) << endl;
+                    fTxSocket->Close();
+                    continue;
+                }
+                timeout.Start(fTimeout);
+                continue;
+
+            case kFALSE: // Time out, no data yet, go on waiting
+              if (timeout.HasTimedOut())
+              {
+                  gLog << warn << MTime(-1) << " WARNING - Connection to " << MTcpIpO::GetSocketAddress(*fTxSocket) << " timed out after " << fTimeout << "ms." << endl;
+                  fTxSocket->Close();
+                  continue;
+              }
+              continue;
+
+            default:  // Error occurance
+                gLog << err << "TSocket::Select returned an error: " << strerror(errno) << endl;
+                fTxSocket->Close();
+                continue;
+            }
+        }
+
+        fConnectionEstablished = kFALSE;
+
+#ifdef DEBUG
+        cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
+#endif
+
+        fMutex.Lock();
+
+        delete fTxSocket;
+
+        fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
+        fTxSocket->SetOption(kNoBlock, 1);
+
+        fMutex.UnLock();
+
+        timeout.Start(fTimeout);
+
+        if (fTxSocket->IsValid())
+            continue;
+
+        MThread::Sleep(wait); // Wait a ms
+        if (wait<3000000) // 3s
             wait *= 2;
     }
Index: trunk/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- trunk/Cosy/tcpip/MTcpIpIO.h	(revision 10026)
+++ trunk/Cosy/tcpip/MTcpIpIO.h	(revision 10031)
@@ -46,5 +46,5 @@
 class MTcpIpO : public MThread
 {
-private:
+protected:
     TSocket *fTxSocket;
     TMutex   fMutex;
@@ -73,8 +73,21 @@
 public:
     MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
-    ~MTcpIpIO();
 
     virtual bool InterpreteStr(TString str);
 };
 
+class MTcpIpOI : public MTcpIpO
+{
+private:
+    Int_t Thread();
+    virtual Bool_t ReadSocket(TSocket &rx) = 0;
+
+    Bool_t fConnectionEstablished;
+    Int_t  fTimeout;    // [ms] Timeout to listen for data to be received
+public:
+    MTcpIpOI(const char *addr, Int_t tx, UInt_t timeout=5000) : MTcpIpO(addr, tx), fConnectionEstablished(kFALSE), fTimeout(timeout) { }
+
+    Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
+};
+
 #endif
