Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 11090)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 11091)
@@ -43,4 +43,5 @@
 extern void factStat(int severity, int err, char* message ) ;
 
+extern int  eventCheck( PEVNT_HEADER *fadhd, EVENT *event) ;
 
 extern void debugRead(int isock, int ibyte, int32_t event, int state, 
@@ -295,5 +296,5 @@
 
    
-   needmem = sizeof(EVENT) + NPIX*nRoi*2 + NTMARK*nRoi*2 ;
+   needmem = sizeof(EVENT) + NPIX*nRoi*2 + NTMARK*nRoi*2; //
 
    headmem = NBOARDS* sizeof(PEVNT_HEADER) ;
@@ -576,5 +577,5 @@
            snprintf(str,MXSTR,"Socket %d closed by FAD",i);
            factOut(kInfo,441, str ) ;
-           j = GenSock(1, i, 0,NULL, &rd[i]) ;
+           GenSock(1, i, 0,NULL, &rd[i]) ;
            gi_ErrCnt[i]++ ;
            gi_NumConnect[ b ]-- ;
@@ -852,14 +853,8 @@
  snprintf(str,MXSTR,"close all sockets ...");
  factOut(kInfo,-1, str ) ;
- for (i=0; i<MAX_SOCK; i++)    
-    if (rd[i].sockStat ==0 ) {  
-       j=close(rd[i].socket) ;
-       if (j>0) {
-          snprintf(str,MXSTR,"Error closing socket %d | %m",i);
-          factOut(kFatal,771, str ) ;
-       }
-       rd[i].sockStat = -1 ;        //flag (try to reopen next round)
-       gi_NumConnect[ i/7 ]-- ;
-    }
+ for (i=0; i<MAX_SOCK; i++) {  
+    GenSock(-1, i, 0, NULL, &rd[i]) ; //close and destroy socket   
+    gi_NumConnect[ i/7 ]-- ;
+ }
 
  xwait.tv_sec = 0;
@@ -944,7 +939,7 @@
         if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] <500) {
            int      id   = evtCtrl.evtBuf[k0] ;
-           uint32_t irun = mBuffer[id].runNum ;
            int      ievt = mBuffer[id].evNum ;
            int      roi  = mBuffer[id].nRoi ;
+//         uint32_t irun = mBuffer[id].runNum ;
 //snprintf(str,MXSTR,"P processing %d %d %d %d",ievt,k,id,evtCtrl.evtStat[k0]) ;
 //factOut(kDebug,-1, str ) ;
@@ -964,16 +959,25 @@
              }
            }
-//and set correct event header ; also check for consistency in event
-          mBuffer[id].fEvent->Roi = roi ;
-          mBuffer[id].fEvent->EventNum = ievt ;
-          mBuffer[id].fEvent->TriggerType = 0 ; // TBD
-          mBuffer[id].fEvent->SoftTrig = 0 ;
-          for (ib=0; ib<NBOARDS; ib++) {
-             mBuffer[id].fEvent->BoardTime[ib] = 123 ;
-
-
-
-
+
+
+//and set correct event header ; also check for consistency in event (not yet)
+           mBuffer[id].fEvent->Roi = roi ;
+           mBuffer[id].fEvent->EventNum = ievt ;
+           mBuffer[id].fEvent->TriggerType = 0 ; // TBD
+           mBuffer[id].fEvent->SoftTrig = 0 ;
+           for (ib=0; ib<NBOARDS; ib++) {
+              if (mBuffer[id].board[ib] == -1 ) {  //board is not read
+                 mBuffer[id].FADhead[ib].start_package_flag = 0 ;
+                 mBuffer[id].fEvent->BoardTime[ib] = 0 ;
+              } else {
+                 mBuffer[id].fEvent->BoardTime[ib] = 
+                 ntohl(mBuffer[id].FADhead[ib].time) ;
+              }
            }
+
+           int i=eventCheck(mBuffer[id].FADhead,mBuffer[id].fEvent) ;
+           
+           if (i<0) evtCtrl.evtStat[k0] = 999 ; //flag event to be skipped
+
            numProc++ ;
            evtCtrl.evtStat[k0] = 520 ;
@@ -1306,5 +1310,5 @@
 
 
-
+/*
      xwait.tv_sec = 20;;
      xwait.tv_nsec= 0 ;  // sleep for ~20sec
@@ -1324,5 +1328,5 @@
 
      g_runStat = -1 ;
-
+*/
 
 
@@ -1371,4 +1375,17 @@
 
 
+
+int  eventCheck( PEVNT_HEADER *fadhd, EVENT *event) 
+{
+   int i=0;
+
+   printf("------------%d\n",ntohl(fadhd[7].fad_evt_counter) );
+   for (i=0; i<NBOARDS; i++) {
+      printf("b=%2d,=%5d\n",i,fadhd[i].board_id);
+   }
+   return 0;
+}
+
+
 void factStat(int severity, int err, char* message ) {
   printf("%3d %3d : %s\n",severity,err,message) ;
@@ -1377,5 +1394,5 @@
 
 void debugRead(int isock, int ibyte, int32_t event, int state, uint32_t tsec, uint32_t tusec ) {
-  printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
+//  printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
 }
 
Index: /trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11090)
+++ /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11091)
@@ -747,4 +747,7 @@
         Update(fDimCurrentEvent, uint32_t(0));
         Update(fDimEvents,       fNumEvts);
+
+        for (size_t i=0; i<40; i++)
+            ConnectSlot(i, tcp::endpoint());
     }
     ~EventBuilderWrapper()
@@ -777,5 +780,5 @@
     }
 
-    void Start(const vector<tcp::endpoint> &addr)
+    void StartThread(const vector<tcp::endpoint> &addr)
     {
         if (IsThreadRunning())
@@ -785,29 +788,8 @@
         }
 
-        int cnt = 0;
         for (size_t i=0; i<40; i++)
-        {
-            if (addr[i]==tcp::endpoint())
-            {
-                g_port[i].sockDef = -1;
-                continue;
-            }
-
-            // -1:  if entry shell not be used
-            //  0:  event builder will connect but ignore the events
-            //  1:  event builder will connect and build events
-            g_port[i].sockDef                  = 1;
-
-            g_port[i].sockAddr.sin_family      = AF_INET;
-            g_port[i].sockAddr.sin_addr.s_addr = htonl(addr[i].address().to_v4().to_ulong());
-            g_port[i].sockAddr.sin_port        = htons(addr[i].port());
-
-            cnt++;
-        }
-
-//        g_maxBoards = cnt;
-        g_actBoards = cnt;
-
-        g_runStat   = kModeRun;
+            ConnectSlot(i, addr[i]);
+
+        g_runStat = kModeRun;
 
         fMsg.Message("Starting EventBuilder thread");
@@ -815,4 +797,43 @@
         fThread = boost::thread(StartEvtBuild);
     }
+    void ConnectSlot(unsigned int i, const tcp::endpoint &addr)
+    {
+        if (i>39)
+            return;
+
+        if (addr==tcp::endpoint())
+        {
+            DisconnectSlot(i);
+            return;
+        }
+
+        g_port[i].sockAddr.sin_family      = AF_INET;
+        g_port[i].sockAddr.sin_addr.s_addr = htonl(addr.address().to_v4().to_ulong());
+        g_port[i].sockAddr.sin_port        = htons(addr.port());
+        // In this order
+        g_port[i].sockDef = 1;
+    }
+    void DisconnectSlot(unsigned int i)
+    {
+        if (i>39)
+            return;
+
+        g_port[i].sockDef = 0;
+        // In this order
+        g_port[i].sockAddr.sin_family      = AF_INET;
+        g_port[i].sockAddr.sin_addr.s_addr = 0;
+        g_port[i].sockAddr.sin_port        = 0;
+    }
+    void IgnoreSlot(unsigned int i)
+    {
+        if (i>39)
+            return;
+        if (g_port[i].sockAddr.sin_port==0)
+            return;
+
+        g_port[i].sockDef = -1;
+    }
+
+
     void Abort()
     {
@@ -848,6 +869,6 @@
     int  GetNumConnected(int i) const { return gi_NumConnect[i]; }
 
-    void SetIgnore(int i, bool b) const { if (g_port[i].sockDef>=0) g_port[i].sockDef=b?0:1; }
-    bool IsIgnored(int i) const { return g_port[i].sockDef==0; }
+    void SetIgnore(int i, bool b) const { if (g_port[i].sockDef!=0) g_port[i].sockDef=b?-1:1; }
+    bool IsIgnored(int i) const { return g_port[i].sockDef==-1; }
 
     void SetDebugStream(bool b) { fDebugStream = b; if (!b) for (int i=0; i<40; i++) fDumpStream[i].close(); }
@@ -1127,9 +1148,7 @@
     }
 
-    /*
-    void message(int severity, const char *msg)
-    {
-        EventBuilderWrapper::This->Update(msg, severity);
-    }*/
+    int eventCheck(PEVNT_HEADER *fadhd, EVENT *event)
+    {
+    }
 }
 
Index: /trunk/FACT++/src/fadctrl.cc
===================================================================
--- /trunk/FACT++/src/fadctrl.cc	(revision 11090)
+++ /trunk/FACT++/src/fadctrl.cc	(revision 11091)
@@ -1000,13 +1000,10 @@
         const int16_t slot = evt.GetShort();
 
-        const BoardList::iterator it = fBoards.find(slot);
+        const BoardList::iterator it = GetSlot(slot);
 
         if (it==fBoards.end())
-        {
-            ostringstream str;
-            str << "Slot " << slot << " not found.";
-            T::Warn(str.str());
             return T::GetCurrentState();
-        }
+
+        ConnectSlot(slot, tcp::endpoint());
 
         delete it->second.second;
@@ -1042,5 +1039,5 @@
             case 0:  str << "1-7:not connected)"; break;
             case 7:  str << "1-7:connected)";     break;
-            default: str << "1-7:connecting [" << fStatus2[idx] << "])"; break;
+            default: str << "1-7:connecting [" << (int)fStatus2[idx] << "])"; break;
             }
 
@@ -1064,5 +1061,5 @@
     }
 
-    void EnableSlot(Connection &c, bool enable=true)
+    void EnableConnection(Connection &c, bool enable=true)
     {
         ConnectionFAD *ptr = c.second;
@@ -1071,5 +1068,7 @@
 
         if (!enable)
+        {
             ptr->PostClose(false);
+        }
         else
         {
@@ -1084,8 +1083,45 @@
     {
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            EnableSlot(i->second, enable);
-    }
-
-    int Disconnect()
+            EnableConnection(i->second, enable);
+    }
+
+    int CloseOpenFiles()
+    {
+        EventBuilderWrapper::CloseOpenFiles();
+        return T::GetCurrentState();
+    }
+
+    int EnableSlot(const EventImp &evt, bool enable)
+    {
+        if (!CheckEventSize(evt.GetSize(), "EnableSlot", 2))
+            return T::kSM_FatalError;
+
+        const int16_t slot = evt.GetShort();
+
+        const BoardList::iterator it = GetSlot(slot);
+
+        if (it==fBoards.end())
+            return T::GetCurrentState();
+
+        EnableConnection(it->second, enable);
+        ConnectSlot(it->first, enable ? it->second.first : tcp::endpoint());
+
+        return T::GetCurrentState();
+    }
+
+    int StartConnection()
+    {
+        vector<tcp::endpoint> addr(40);
+
+        for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
+            addr[i->first] = i->second.first;
+
+        StartThread(addr);
+        EnableAll(true);
+
+        return T::GetCurrentState();
+    }
+
+    int StopConnection()
     {
         Exit();
@@ -1094,27 +1130,8 @@
     }
 
-    int ForceDisconnect()
+    int AbortConnection()
     {
         Abort();
         EnableAll(false);
-        return T::GetCurrentState();
-    }
-
-    int CloseOpenFiles()
-    {
-        EventBuilderWrapper::CloseOpenFiles();
-        return T::GetCurrentState();
-    }
-
-    int Connect()
-    {
-        vector<tcp::endpoint> addr(40);
-
-        for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            addr[i->first] = i->second.first;
-
-        Start(addr);
-        EnableAll(true);
-
         return T::GetCurrentState();
     }
@@ -1134,4 +1151,5 @@
         // ===== Evaluate connection status =====
 
+        uint16_t nclosed1     = 0;
         uint16_t nconnecting1 = 0;
         uint16_t nconnecting2 = 0;
@@ -1151,4 +1169,9 @@
             {
                 const ConnectionFAD *c = slot->second.second;
+                if (c->IsDisconnected())
+                {
+                    stat1[idx] = 0;
+                    nclosed1++;
+                }
                 if (c->IsConnecting())
                 {
@@ -1188,11 +1211,13 @@
         // fadctrl:       Always connecting if not disabled
         // event builder:
-
-       // FIXME: Evaluate event builder status
-         if (nconnected1==cnt && nconnected2==cnt && cnt>0)
+        if (nconnecting1==0 && nconnected1>0 &&
+            nconnected2==nconnected1)
             return FAD::kConnected;
 
-        if (nconnected1!=0 || nconnected2!=0)
+        if (nconnecting1>0 || nconnecting2>0 || nconnected1!=nconnected2)
             return FAD::kConnecting;
+
+//        cout << 1 << " " << nconnecting1 << " " << nconnected1 << " " << nclosed1 << endl;
+//        cout << 2 << " " << nconnecting2 << " " << nconnected2 << endl;
 
         // nconnected1 == nconnected2 == 0
@@ -1248,5 +1273,5 @@
         T(out, "FAD_CONTROL"), EventBuilderWrapper(*static_cast<MessageImp*>(this)), ba::io_service::work(static_cast<ba::io_service&>(*this)),
         fStatus1(40), fStatus2(40),
-        fDimConnection("FAD_CONTROL/CONNECTIONS", "C:41", "")
+        fDimConnection("FAD_CONTROL/CONNECTIONS", "C:40", "")
     {
         // ba::io_service::work is a kind of keep_alive for the loop.
@@ -1308,8 +1333,8 @@
             (boost::bind(&StateMachineFAD::SendTriggers, this, _1))
             ("Issue software triggers");
-        T::AddEvent("START", "")
+        T::AddEvent("START_RUN", "")
             (boost::bind(&StateMachineFAD::StartRun, this, _1, true))
             ("Set FAD DAQ mode. when started, no configurations must be send.");
-        T::AddEvent("STOP")
+        T::AddEvent("STOP_RUN")
             (boost::bind(&StateMachineFAD::StartRun, this, _1, false))
             ("");
@@ -1410,19 +1435,21 @@
 
         // Conenction commands
-        /*
-        T::AddEvent("ENABLE", "S:1;B:1", FAD::kDisconnected)
-            (boost::bind(&StateMachineFAD::Enable, this, _1))
-            ("");*/
-
-        T::AddEvent("CONNECT", FAD::kOffline)
-            (boost::bind(&StateMachineFAD::Connect, this))
+        T::AddEvent("START", FAD::kOffline)
+            (boost::bind(&StateMachineFAD::StartConnection, this))
             ("");
-
-        T::AddEvent("DISCONNECT", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
-            (boost::bind(&StateMachineFAD::Disconnect, this))
+        T::AddEvent("STOP",  FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
+            (boost::bind(&StateMachineFAD::StopConnection, this))
             ("");
 
-        T::AddEvent("FORCE_DISCONNECT", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
-            (boost::bind(&StateMachineFAD::ForceDisconnect, this))
+        T::AddEvent("ABORT", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
+            (boost::bind(&StateMachineFAD::AbortConnection, this))
+            ("");
+
+        T::AddEvent("CONNECT", "S:1", FAD::kConnecting, FAD::kConnected)
+            (boost::bind(&StateMachineFAD::EnableSlot, this, _1, true))
+            ("");
+
+        T::AddEvent("DISCONNECT", "S:1", FAD::kConnecting, FAD::kConnected)
+            (boost::bind(&StateMachineFAD::EnableSlot, this, _1, false))
             ("");
 
@@ -1503,5 +1530,5 @@
                 AddEndpoint(tcp::endpoint(endpoint.address(), endpoint.port()+8*i));
 
-            Connect();
+            StartConnection();
             return true;
         }
@@ -1554,5 +1581,5 @@
         }
 
-        Connect();
+        StartConnection();
 
         return true;
