title: Asio end socket functions: cancel, shutdown, close, release
body: <p>Which is the correct way to close and clean up a socket?</p>
  
  <p>I have the io_service running in a secondary thread and I need to close the connection from the main thread:</p>
  
  <pre><code>void closeConnection()
  {
      ioc.post([&amp;socket]() {
          // Which ones do I have to call?
          // In what order?
          // What do they do?
          //socket.cancel();
          //socket.shutdown(asio::ip::tcp::socket::shutdown_both);
          //socket.close();
          //socket.release();
      });
      secondaryThread.join();
  }
  </code></pre>
  
  <p>What is the difference between all these functions?</p>
  
  <p>I've tried with this sequence ...</p>
  
  <pre><code>socket.cancel();
  socket.close();
  socket.release();
  </code></pre>
  
  <p>and seems to close up the connection without errors but takes too much time (about 5-10 seconds), so I guess I'm doing something wrong.</p>