Index: /trunk/FACT++/src/Connection.cc
===================================================================
--- /trunk/FACT++/src/Connection.cc	(revision 11189)
+++ /trunk/FACT++/src/Connection.cc	(revision 11190)
@@ -3,4 +3,7 @@
 
 @brief Maintains an ansynchronous TCP/IP client connection
+
+@todo
+   Unify with ConnectionUSB
 
 */
@@ -44,4 +47,5 @@
 }
 
+/*
 void Connection::AsyncWait(ba::deadline_timer &timer, int millisec,
                            void (Connection::*handler)(const bs::error_code&))
@@ -59,4 +63,5 @@
     timer.async_wait(boost::bind(handler, this, dummy::error));
 }
+*/
 
 void Connection::AsyncConnect(tcp::resolver::iterator iterator)
@@ -66,7 +71,17 @@
     // AsyncConnect + Deadline
      async_connect(endpoint,
-                  boost::bind(&Connection::ConnectImp,
-                              this, ba::placeholders::error,
-                              iterator));
+                  boost::bind(&Connection::ConnectIterImp,
+                              this, iterator, ba::placeholders::error));
+
+    // We will get a "Connection timeout anyway"
+    //AsyncWait(fConnectTimeout, 5, &Connection::HandleConnectTimeout);
+}
+
+void Connection::AsyncConnect()
+{
+    // AsyncConnect + Deadline
+     async_connect(fEndpoint,
+                  boost::bind(&Connection::ConnectAddrImp,
+                              this, fEndpoint, ba::placeholders::error));
 
     // We will get a "Connection timeout anyway"
@@ -275,9 +290,6 @@
 }
 
-void Connection::ConnectImp(const bs::error_code& error,
-                            tcp::resolver::iterator iterator)
-{
-    tcp::endpoint endpoint = *iterator;
-
+void Connection::ConnectAddrImp(const tcp::endpoint &endpoint, const bs::error_code& error)
+{
     const string host = endpoint.port()==0 ? "" :
         endpoint.address().to_string()+":"+lexical_cast<string>(endpoint.port());
@@ -325,5 +337,5 @@
 
     fConnectionStatus = kConnecting;
-
+/*
     // Go on with the next
     if (++iterator != tcp::resolver::iterator())
@@ -332,5 +344,5 @@
         return;
     }
-
+*/
     // No more entries to try, if we would not put anything else
     // into the queue anymore it would now return (run() would return)
@@ -343,8 +355,88 @@
 }
 
+void Connection::ConnectIterImp(tcp::resolver::iterator iterator, const bs::error_code& error)
+{
+    ConnectAddrImp(*iterator, error);
+/*
+    tcp::endpoint endpoint = *iterator;
+
+    const string host = endpoint.port()==0 ? "" :
+        endpoint.address().to_string()+":"+lexical_cast<string>(endpoint.port());
+
+    // Connection established
+    if (!error)
+    {
+	set_option(socket_base::keep_alive(true));
+
+	const int optval = 10;
+        // First keep alive after 10s
+	setsockopt(native(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
+        // New keep alive after 10s
+	setsockopt(native(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
+
+	Info("Connection established to "+host+"...");
+
+        fConnectionStatus = kConnected;
+
+        ConnectionEstablished();
+        return;
+    }
+
+    // If returning from run will lead to deletion of this
+    // instance, close() is not needed (maybe implicitly called).
+    // If run is called again, close() is needed here. Otherwise:
+    // Software caused connection abort when we try to resolve
+    // the endpoint again.
+    CloseImp(false);
+
+    ostringstream msg;
+    if (!host.empty())
+        msg << "Connecting to " << host << ": " << error.message() << " (" << error << ")";
+
+    if (fErrConnect!=msg.str())
+    {
+        if (error!=ba::error::basic_errors::connection_refused)
+            fMsgConnect = "";
+        fErrConnect = msg.str();
+        Warn(fErrConnect);
+    }
+
+    if (error==ba::error::basic_errors::operation_aborted)
+        return;
+
+    fConnectionStatus = kConnecting;
+*/
+    // Go on with the next
+    if (++iterator != tcp::resolver::iterator())
+    {
+        AsyncConnect(iterator);
+        return;
+    }
+/*
+    // No more entries to try, if we would not put anything else
+    // into the queue anymore it would now return (run() would return)
+
+    // Since we don't want to block the main loop, we wait using an
+    // asnychronous timer
+
+    // FIXME: Should we move this before AsyncConnect() ?
+    AsyncWait(fConnectionTimer, 250, &Connection::HandleConnectionTimer);
+*/
+}
+
 // FIXME: Async connect should get address and port as an argument
 void Connection::StartConnect()
 {
     fConnectionStatus = kConnecting;
+
+    if (fEndpoint!=tcp::endpoint())
+    {
+        ostringstream msg;
+        msg << "Trying to connect to " << fEndpoint << "...";
+        Info(msg);
+
+        AsyncConnect();
+        return;
+    }
 
     tcp::resolver resolver(get_io_service());
Index: /trunk/FACT++/src/Connection.h
===================================================================
--- /trunk/FACT++/src/Connection.h	(revision 11189)
+++ /trunk/FACT++/src/Connection.h	(revision 11190)
@@ -4,4 +4,5 @@
 #include <deque>
 #include <string>
+#include <boost/bind.hpp>
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
@@ -17,4 +18,6 @@
     std::string fAddress;
     std::string fPort;
+
+    boost::asio::ip::tcp::endpoint fEndpoint;
 
     bool fDebugTx;
@@ -48,14 +51,39 @@
     void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
     void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
+
+    template<class T>
+    void AsyncWaitImp(boost::asio::deadline_timer &timer, int millisec,
+                      void (T::*handler)(const boost::system::error_code&))
+    {
+        // - The boost::asio::basic_deadline_timer::expires_from_now()
+        //   function cancels any pending asynchronous waits, and returns
+        //   the number of asynchronous waits that were cancelled. If it
+        //   returns 0 then you were too late and the wait handler has
+        //   already been executed, or will soon be executed. If it
+        //   returns 1 then the wait handler was successfully cancelled.
+        // - If a wait handler is cancelled, the bs::error_code passed to
+        //   it contains the value bs::error::operation_aborted.
+        timer.expires_from_now(boost::posix_time::milliseconds(millisec));
+
+        timer.async_wait(boost::bind(handler, this, boost::asio::placeholders::error));
+    }
+
     void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
-                   void (Connection::*handler)(const boost::system::error_code&));
+                   void (Connection::*handler)(const boost::system::error_code&))
+    {
+        AsyncWaitImp(timer, millisec, handler);
+    }
+
 
 private:
     void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
+    void AsyncConnect();
 
     void CloseImp(bool restart=true);
 
-    void ConnectImp(const boost::system::error_code& error,
-                    boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
+    void ConnectAddrImp(const boost::asio::ip::tcp::endpoint &endpoint,
+                        const boost::system::error_code& error);
+    void ConnectIterImp(boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
+                        const boost::system::error_code& error);
 
     void HandleConnectionTimer(const boost::system::error_code &error);
@@ -75,4 +103,5 @@
     void SetEndpoint(const std::string &addr, const std::string &port);
     void SetEndpoint(const std::string &addr);
+    void SetEndpoint(const boost::asio::ip::tcp::endpoint &ep) { fEndpoint = ep; }
 
     void StartConnect();
@@ -112,4 +141,6 @@
 
     std::string URL() const { return fAddress + ":" + fPort; }
+
+    const boost::asio::ip::tcp::endpoint &GetEndpoint() const { return fEndpoint; }
 };
 
