Index: trunk/MagicSoft/Mars/mbase/MEvtLoop.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 2490)
@@ -96,12 +96,4 @@
 using namespace std;
 
-
-//!
-//! Maybe we can add a static parameter list to MEvtLoop
-//! Also we can derive MEvtLoop from MTaskList to have a static tasklist, too
-//!
-
-TList *gListOfPrimitives; // forward declaration in MParContainer.h
-
 // --------------------------------------------------------------------------
 //
@@ -372,5 +364,4 @@
     if (fProgress && fNumEvents>0)
         fProgress->SetPosition((Double_t)num/fNumEvents);
-
 
     // FIXME: This is a workaround, because TApplication::Run is not
Index: trunk/MagicSoft/Mars/mbase/MEvtLoop.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MEvtLoop.h	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MEvtLoop.h	(revision 2490)
@@ -85,6 +85,3 @@
 };
 
-// FIXME: Move as (persistent) static data member to MParContainer
-R__EXTERN TList *gListOfPrimitives; // instantiation in MEvtLoop
-
 #endif
Index: trunk/MagicSoft/Mars/mbase/MGGroupFrame.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MGGroupFrame.h	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MGGroupFrame.h	(revision 2490)
@@ -14,5 +14,5 @@
 class MGGroupFrame : public TGGroupFrame, public TGWidget
 {
-    MGTask  *fTask;
+    MGTask *fTask;
     MGList *fList;
 
Index: trunk/MagicSoft/Mars/mbase/MParContainer.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MParContainer.cc	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MParContainer.cc	(revision 2490)
@@ -51,9 +51,5 @@
 #include "MLogManip.h"
 
-#ifndef __COSY__
-#include "MEvtLoop.h"     // gListOfPrimitives
-#else
 TList *gListOfPrimitives; // forard declaration in MParContainer.h
-#endif
 
 #undef DEBUG
Index: trunk/MagicSoft/Mars/mbase/MParContainer.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MParContainer.h	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MParContainer.h	(revision 2490)
@@ -132,4 +132,12 @@
 };
 
+//!
+//! Maybe we can add a static parameter list to MEvtLoop
+//! Also we can derive MEvtLoop from MTaskList to have a static tasklist, too
+//!
+
+// FIXME: Move as (persistent) static data member to MParContainer
+R__EXTERN TList *gListOfPrimitives; // instantiation in MEvtLoop
+
 /*
 class MParContainer : public TNamed
Index: trunk/MagicSoft/Mars/mbase/MReadSocket.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MReadSocket.cc	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MReadSocket.cc	(revision 2490)
@@ -16,7 +16,7 @@
 !
 !
-!   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@uni-sw.gwdg.de>
-!
-!   Copyright: MAGIC Software Development, 2000-2001
+!   Author(s): Thomas Bretz, 10/2003 <mailto:tbretz@astro.uni.wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2003
 !
 !
@@ -27,4 +27,25 @@
 //
 // MReadSocket
+//
+// This class acts like a standard C++ istream, but read from a socket
+// (ifstream works similar)
+//
+// ios::io_state:
+// --------------
+// eof()  or ios::eofbit:  Connection closed or not established
+// fail() or ios::failbit: Error trying to establish connection or
+//                         waiting for data in underflow() timed out
+// good() tells you that everything is ok and we can read from the stream
+//
+// Example:
+// --------
+//
+//    Double_t d;
+//
+//    MReadSocket sin(1024); // open port 1024
+//
+//    sin >> d;
+//    sin.read((char*)&d, sizeof(Double_t));
+//
 //
 //////////////////////////////////////////////////////////////////////////////
@@ -44,4 +65,16 @@
 using namespace std;
 
+// --------------------------------------------------------------------------
+//
+// You can use the constructor in two ways:
+//
+//    MReadSocket read(7000);
+// This opens the socket and blocks until the connection has been
+// established.
+//
+//    MReadSocket read;
+// Returns immidiatly. The connection will be opend by calling
+//    read.Open(7000);
+//
 MReadSocket::MReadSocket(int port, int mtu) : istream(this), fMtu(mtu), fTimeout(2500), fServSock(NULL), fRxSocket(NULL)
 {
@@ -50,16 +83,37 @@
     setg(fBuffer, fBuffer, fBuffer+1);
 
-    cout << "Starting server socket on port 7000..." << endl;
-
-    while (1)
+    clear(ios::eofbit);
+
+    if (port>0)
+        Open(port);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Destructor. Close an possible open connection and delete the fBuffer
+//
+MReadSocket::~MReadSocket()
+{
+    Close();
+    delete fBuffer;
+}
+
+void MReadSocket::OpenServerSocket(int port)
+{
+    if (fServSock)
+        return;
+
+    cout << "Starting server socket on port #" << port << "..." << endl;
+
+    while (!fServSock)
     {
         fServSock=new TServerSocket(port, kTRUE);
         if (fServSock->IsValid())
-            break;
+            continue;
 
         cout << "ServerSocket not valid: ";
         switch (fServSock->GetErrorCode())
         {
-        case 0: cout << "No error." << endl; break;
+        case  0: cout << "No error." << endl; break;
         case -1: cout << "low level socket() call failed." << endl; break;
         case -2: cout << "low level bind() call failed." << endl; break;
@@ -68,71 +122,144 @@
         }
 
-        delete fServSock;
-        fServSock=NULL;
-        break;
-    }
-    if (!fServSock)
-    {
-        cout << "MReadSocket: TServerSocket - Connection timed out." << endl;
-        setstate(ios::failbit);
+        Close();
+        clear(ios::failbit);
         return;
     }
 
     fServSock->SetOption(kNoBlock, 1);
-
-    while (1)
+}
+
+void MReadSocket::OpenConnection(Bool_t block)
+{
+    do
     {
         const TTime timeout = gSystem->Now() + TTime(5000);
 
         TDatime now;
-        cout << now.AsString() << ": Waiting for conntection on port 7000..." << endl;
-        fRxSocket = NULL;
-        while ((Long_t)fRxSocket<=0 && gSystem->Now()<timeout)
+        cout << now.AsString() << ": Waiting for connection..." << endl;
+
+        //
+        // fRxSocket<0  means: No connection,non-blocking mode
+        // fRxSocket==0 means: Error
+        // This is only done until timeout is reached
+        //
+        while (fRxSocket==0 && gSystem->Now()<timeout)
         {
             fRxSocket = fServSock->Accept();
             if (fRxSocket==0)
-                cout << "Error: TServerSock::Accept" << endl;
-            usleep(1);
+            {
+                cout << "MReadSocket::OpenConnection: ERROR - TServerSock::Accept()" << endl;
+                setstate(ios::failbit);
+                return;
+            }
+            if ((Long_t)fRxSocket<0)
+                fRxSocket=NULL;
+
+            usleep(10);
         }
 
-        if ((Long_t)fRxSocket<=0)
+        //
+        // No connection has been established. Restart waiting for
+        // connection except we are in non-blocking mode.
+        //
+        if (fRxSocket==0)
             continue;
 
+        //
+        // Check if the established connection is valid
+        //
         if (fRxSocket->IsValid())
-            break;
+        {
+            cout << "Connection established..." << endl;
+            fRxSocket->SetOption(kNoBlock, 1);
+            clear();
+            return;
+        }
 
         cout << "TSocket: Connection not valid..." << endl;
         delete fRxSocket;
-    }
-
-    if ((Long_t)fRxSocket<=0)
-    {
-        cout << "MReadSocket: TServerSocket::Accept - Connection timed out." << endl;
         fRxSocket=NULL;
         setstate(ios::failbit);
         return;
-    }
-
-    cout << "Connection established..." << endl;
-
-    fRxSocket->SetOption(kNoBlock, 1);
+
+    } while (block);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Open the connectionj on port port. Wait until the connection has
+//  been established. If an error occures and the connection cannot
+//  be established return kFALSE. To check whether an error occured
+//  use operator!() or operator void*() or fail()
+//
+Bool_t MReadSocket::Open(int port, Bool_t block=kFALSE)
+{
+    //
+    // If no port is given use the port given in the constructor
+    //
+    if (port<=0)
+        port = fPort;
+
+    //
+    // Remember port for later uses
+    //
+    if (fPort<=0)
+        fPort = port;
+
+    //
+    // Check whether a connection has already been established
+    //
+    if (fServSock)
+    {
+        //
+        // Check whether the connection has the right port
+        //
+        if (fServSock->GetLocalPort()!=port)
+            Close();
+    }
+
+    //
+    // Check whether port is valid
+    //
+    if (port<=0)
+    {
+        cout << "Invalid port #" << port << "!" << endl;
+        clear(ios::failbit);
+        return kFALSE;
+    }
+
+    //
+    // Start server socket...
+    //
+    OpenServerSocket(port);
+    if (!fServSock)
+        return kFALSE;
+
+    OpenConnection(block);
+    if (!fRxSocket)
+        return kFALSE;
 
     underflow();
-}
-
-// --------------------------------------------------------------------------
-//
-//  Destructor, destroying the gui mutex.
-//
-MReadSocket::~MReadSocket()
+    return kTRUE;
+}
+
+void MReadSocket::Close()
 {
     if (fRxSocket)
+    {
         delete fRxSocket;
+        fRxSocket=NULL;
+    }
     if (fServSock)
+    {
+        const Int_t port = fServSock->GetLocalPort();
+
         delete fServSock;
-
-    delete fBuffer;
-
-    cout << "Connection on Port 7000 closed." << endl;
+        fServSock=NULL;
+
+        cout << "Connection on Port #" << port << " closed." << endl;
+    }
+
+    clear(ios::eofbit);
 }
 
@@ -149,5 +276,6 @@
 int MReadSocket::underflow()
 {
-    if (fail())
+    // FIXME:     vvvvv is this correct?
+    if (fail() || eof())
     {
         setg(fBuffer, fBuffer, fBuffer+fMtu);
@@ -185,3 +313,2 @@
     return 0;
 }
-
Index: trunk/MagicSoft/Mars/mbase/MReadSocket.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MReadSocket.h	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MReadSocket.h	(revision 2490)
@@ -19,4 +19,5 @@
     char *fBuffer; //!
 
+    int   fPort;
     int   fMtu;
     TTime fTimeout;
@@ -25,13 +26,19 @@
     TSocket        *fRxSocket;
 
+    void OpenServerSocket(int port);
+    void OpenConnection(Bool_t block);
+
     int underflow();
     int sync();
 
 public:
-    MReadSocket(int port, int mtu=1500);
+    MReadSocket(int port=-1, int mtu=1500);
     MReadSocket(MReadSocket const& log) : istream((std::streambuf*)&log)
     {
     }
     ~MReadSocket();
+
+    Bool_t Open(int port=-1, Bool_t block=kFALSE);
+    void   Close();
 
     void SetTimeout(UInt_t millisec) { fTimeout = millisec; }
Index: trunk/MagicSoft/Mars/mbase/MTask.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MTask.cc	(revision 2473)
+++ trunk/MagicSoft/Mars/mbase/MTask.cc	(revision 2490)
@@ -83,5 +83,4 @@
 
 #include "MFilter.h"
-#include "MGGroupFrame.h"
 #include "MStatusDisplay.h"
 
