Index: trunk/FACT++/src/fadctrl.cc
===================================================================
--- trunk/FACT++/src/fadctrl.cc	(revision 14700)
+++ trunk/FACT++/src/fadctrl.cc	(revision 14701)
@@ -680,5 +680,5 @@
                 msg << hex << "Channel " << dat[0] << " or Value " << dat[1] << " out of range.";
                 T::Error(msg);
-                return false;
+                return T::GetCurrentState();
             }
 
@@ -700,5 +700,5 @@
                 msg << hex << "Channel " << dat[0] << " or Value " << dat[1] << " out of range.";
                 T::Error(msg);
-                return false;
+                return T::GetCurrentState();
             }
 
@@ -757,5 +757,5 @@
             msg << hex << "Value " << evt.GetUShort() << " out of range, max=" << 0xffff << "(?)";
             T::Error(msg);
-            return false;
+            return T::GetCurrentState();
         }
 
@@ -773,24 +773,17 @@
         const uint64_t num = evt.GetUXtra();
 
-        if (num>FAD::kMaxRunNumber)
+        if (num<=0 || num>FAD::kMaxRunNumber)
         {
             ostringstream msg;
-            msg << "Run number " << num << " out of range (max=" << FAD::kMaxRunNumber << ")";
+            msg << "Run number " << num << " out of range [1;" << FAD::kMaxRunNumber << "]";
             T::Error(msg);
-            return false;
-        }
-
-        if (num>0 && num<GetRunNumber())
-        {
-            ostringstream msg;
-            msg << "Given run number (" << num << ") smaller than next run number (" << GetRunNumber() << ") which will be opened by the event builder";
-            T::Error(msg);
-            return false;
-        }
-
-        IncreaseRunNumber(num);
+            return T::GetCurrentState();
+        }
+
+        if (!IncreaseRunNumber(num))
+            return T::GetCurrentState();
  
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second->CmdSetRunNumber(num);
+            i->second->CmdSetRunNumber(GetRunNumber());
 
         return T::GetCurrentState();
@@ -809,5 +802,5 @@
             msg << hex << "Value " << mem << " out of range.";
             T::Error(msg);
-            return false;
+            return T::GetCurrentState();
         }
 
@@ -838,5 +831,5 @@
 	default:
             T::Error("File format unknonw.");
-            return false;
+            return T::GetCurrentState();
         }
 
@@ -1064,17 +1057,8 @@
         }
 
-        if (fNightAsInt!=Time().NightAsInt())
-        {
-            ostringstream out;
-            out << "Night changed from " << fNightAsInt << " to " << Time().NightAsInt() << "... determining new run-number.";
-            T::Info(out);
-
-            // FIXME: What about an error state?
-            fNightAsInt = InitRunNumber();
-            if (fNightAsInt<0)
-                return FAD::State::kConnected;
-        }
-
+        // FIXME: What about an error state?
         const uint32_t runno = StartNewRun(evt.Get<int64_t>(), evt.Get<int64_t>(8), *fTargetConfig);
+        if (runno==0)
+            return FAD::State::kConnected;
 
         ostringstream str;
@@ -1333,4 +1317,78 @@
         return T::GetCurrentState();
     }
+
+    // ============================================================================
+/*
+    bool ProcessReconnection(const list<ReconnectionSlot>::iterator &it)
+    {
+        auto board = GetSlot(it->slot);
+        if (board==fBoards.end())
+            return false;
+
+        ConnectionFAD *fad  = board->second;
+
+        // ----------------------------------------------
+        // Disconnect
+        // ----------------------------------------------
+        if (it->state==0)
+        {
+            if (!fad->IsConnected())
+                return false;
+
+            EnableConnection(fad, false);
+            ConnectSlot(it->slot, tcp::endpoint());
+
+            it->time  = Time();
+            it->state = 1;
+
+            return true;
+        }
+
+        // ----------------------------------------------
+        // Wait for disconnect or timeout
+        // ----------------------------------------------
+        if (it->state==1)
+        {
+            if (!fad->IsDisconnected() && it->time+boost::posix_time::seconds(10)>Time())
+                return true;
+
+            it->time  = Time();
+            it->state = 2;
+
+            return true;
+        }
+
+        // ----------------------------------------------
+        // Wait for timeout after disconnect / Re-connect
+        // ----------------------------------------------
+        if (it->state==2)
+        {
+            if (it->time+boost::posix_time::seconds(3)>Time())
+                return true;
+
+            EnableConnection(fad, true);
+            ConnectSlot(it->slot, fad->GetEndpoint());
+
+            it->time  = Time();
+            it->state = 3;
+
+            return true;
+        }
+
+        // ----------------------------------------------
+        // Wait for connect or timeout / Re-start
+        // ----------------------------------------------
+        if (!fad->IsConnected() && it->time+boost::posix_time::seconds(10)>Time())
+            return true;
+
+        // 'Fix' the information which got lost during re-connection
+        fad->Cmd(FAD::kCmdBusyOff,     false);
+        fad->Cmd(FAD::kCmdSocket,      false);
+        fad->Cmd(FAD::kCmdTriggerLine, true);
+
+        return false;
+    }
+*/
+    // ============================================================================
 
     vector<uint8_t> fStatus1;
@@ -1430,4 +1488,40 @@
         // ===== Return connection status =====
 
+        // Keep the state during reconnection (theoretically, can only be WritingData)
+        if (fReconnectionList.size()>0)
+        {
+            bool isnew = true;
+            for (auto it=fReconnectionList.begin(); it!=fReconnectionList.end(); it++)
+                if (it->state>0)
+                {
+                    isnew = false;
+                    break;
+                }
+
+            if (isnew)
+            {
+                for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
+                    it->second->Cmd(FAD::kCmdBusyOn, true);  // continously on
+            }
+
+            // Loop over all scheduled re-connections
+            for (auto it=fReconnectionList.begin(); it!=fReconnectionList.end(); it++)
+            {
+                if (ProcessReconnection(it))
+                    continue;
+
+                const lock_guard<mutex> guard(fMutexReconnect);
+                fReconnectionList.erase(it);
+            }
+
+            if (fReconnectionList.size()==0)
+            {
+                for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
+                    it->second->Cmd(FAD::kCmdBusyOff, false);
+            }
+
+            return T::GetCurrentState();
+        }
+
         // fadctrl:       Always connecting if not disabled
         // event builder:
@@ -1477,4 +1571,8 @@
                     //        successfully enabled the trigger lines?
                 }
+
+                const lock_guard<mutex> guard(fMutexReconnect);
+                fReconnectionList.clear();
+
                 return FAD::State::kConfigured;
             }
@@ -1559,5 +1657,5 @@
                                                   "Connection status of FAD boards"
                                                   "|status[bitpattern]:lower bits stat1, upper bits stat2, for every board. 40=thread"
-                                                  "|char[unknown]:to be completed")
+                                                  "|status[bool]:tue or false whether the event builder threads are running")
     {
         // ba::io_service::work is a kind of keep_alive for the loop.
@@ -1905,6 +2003,4 @@
     }
 
-    int64_t fNightAsInt;
-
     int EvalOptions(Configuration &conf)
     {
@@ -1918,6 +2014,5 @@
         SetMaxMemory(conf.Get<unsigned int>("max-mem"));
 
-        fNightAsInt = InitRunNumber(conf.Get<string>("destination-folder"));
-        if (fNightAsInt<0)
+        if (!InitRunNumber(conf.Get<string>("destination-folder")))
             return 1;
 
