title: C++ boost/asio client doesn't connect to server
body: <p>I am learning boost/asio ad wrote 2 programs(client and server) from e-book with minor changes.
  Basically it should connect to my server.
  When i try to connect to outside world(some random http server ) all is good and it works but when i change destination to "localhost:40002" it says invalid argument.</p>
  
  <p>client code:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;iostream&gt;
  
  int main () {
     try {
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::resolver::query query("localhost", 40002);
        boost::asio::ip::tcp::resolver resolver(io_service);
        boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
        boost::asio::ip::tcp::resolver::iterator end ;
        boost::asio::ip::tcp::endpoint endpoint;
  
        while ( destination != end ) {
           endpoint = *destination++;
           std::cout&lt;&lt;endpoint&lt;&lt;std::endl;
        }
  
        boost::asio::ip::tcp::socket socket(io_service);
        socket.connect(endpoint);
     }
     catch (std::exception&amp; e)
     {
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
     }
     return 0;
  }
  </code></pre>
  
  <p>i did "netstat -l" and it showed that i am truly listening to my port so server i think works to but never less they don't connect</p>
  
  <p>server code:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  std::string time_string()
  {
     using namespace std;
     time_t now = time(0);
     return ctime(&amp;now);
  }
  int main () {
  
     try {
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 40002));
  
        for (; ;) {
           std::cout&lt;&lt;"Listening to"&lt;&lt;std::endl;
           boost::asio::ip::tcp::socket socket(io_service);
           acceptor.accept(socket);
  
           std::string message = time_string();
           boost::system::error_code ignored_error;
           boost::asio::write(socket, boost::asio::buffer(message), boost::asio::transfer_all(), ignored_error);
        }
     }
     catch (std::exception&amp; e)
     {
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
     }
     return 0;
  
  }
  </code></pre>
  
  <p>Can someone hint why connection doesn't happen</p>