<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><pre>I am having a timeout example code problem, modified code is below:</pre><pre>class connect_handler
{
public:
  connect_handler(io_service&amp; ios)
    : io_service_(ios),
      timer_(ios),
      socket_(ios)
  {
    socket_.async_connect(
        tcp::endpoint(boost::asio::ip::address_v4::loopback(), 32123),
        boost::bind(&amp;connect_handler::handle_connect, this,
          boost::asio::placeholders::error));

    timer_.expires_from_now(boost::posix_time::seconds(5));
    timer_.async_wait(boost::bind(&amp;connect_handler::close, this));
  }

  void handle_connect(const boost::system::error_code&amp; err)
  {
    if (err)
    {
      std::cout &lt;&lt; "Connect error: " &lt;&lt; err.message() &lt;&lt; "\n";
    }
    else
    {</pre><pre>     // We need to cancel the timeout since we have connected.</pre><pre>     timer_.cancel();<font class="Apple-style-span" face="'Lucida Grande'"><b> // &lt; Fires the timer regardless. Because we are calling cancel() from inside an io_service call???</b></font></pre><pre>      std::cout &lt;&lt; "Successful connection\n";
    }
  }

  void close()
  {
    socket_.close();
  }

private:
  io_service&amp; io_service_;
  deadline_timer timer_;
  tcp::socket socket_;
};</pre></body></html>
