title: socket accepted but m_socket.remote_endpoint raise Transport endpoint is not connected
body: <p>Code:</p>
  
  <p>Listener.hpp:</p>
  
  <pre><code>template &lt;typename SocketType&gt;
  void Listener&lt;SocketType&gt;::BeginAccept()
  {
      auto worker = SelectWorker();
      auto socket = worker-&gt;CreateSocket();
  
      m_acceptor-&gt;async_accept(socket-&gt;GetAsioSocket(),
          [this, worker, socket] (const boost::system::error_code &amp;ec)
      {
          this-&gt;OnAccept(worker, socket, ec);
      });
  }
  
  template &lt;typename SocketType&gt;
  void Listener&lt;SocketType&gt;::OnAccept(NetworkThread&lt;SocketType&gt; *worker, std::shared_ptr&lt;SocketType&gt; const&amp; socket, const boost::system::error_code &amp;ec)
  {
      // an error has occurred
      if (ec)
          worker-&gt;RemoveSocket(socket.get());
      else
          socket-&gt;Open();
  
      BeginAccept();
  }
  </code></pre>
  
  <p>Socket.cpp:</p>
  
  <pre><code>bool Socket::Open()
  {
      try
      {
          const_cast&lt;std::string &amp;&gt;(m_address) = m_socket.remote_endpoint().address().to_string();
          const_cast&lt;std::string &amp;&gt;(m_remoteEndpoint) = boost::lexical_cast&lt;std::string&gt;(m_socket.remote_endpoint());
      }
      catch (boost::system::error_code&amp; error)
      {
          sLog.outInfo("Socket::Open() failed to get remote address.  Error: %s", error.message().c_str());
          return false;
      }
      catch (const std::exception&amp; error)
      {
          sLog.outInfo("Socket::Open() failed(with std::exception) to get remote address.  Error: %s", error.what());
          return false;
      }
      catch (...)
      {
          sLog.outError("Socket::Open() failed to get remote address.  Other error");
          return false;
      }
  
      m_outBuffer.reset(new PacketBuffer);
      m_secondaryOutBuffer.reset(new PacketBuffer);
      m_inBuffer.reset(new PacketBuffer);
  
      StartAsyncRead();
  
      return true;
  }
  </code></pre>
  
  <p>Is this related to TCP attack?</p>
  
  <p>EDIT: gdb:</p>
  
  <pre><code>(gdb) p m_socket
  $1 = {&lt;boost::asio::basic_socket&lt;boost::asio::ip::tcp, boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;&gt; = {&lt;boost::asio::basic_io_object&lt;boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt;, true&gt;&gt; = {
        implementation = {&lt;boost::asio::detail::reactive_socket_service_base::base_implementation_type&gt; = {socket_ = 21, state_ = 80 'P', reactor_data_ = 0x7ffff0005120}, protocol_ = {family_ = 2}}, service_ = 0x7f2b30}, &lt;boost::asio::socket_base&gt; = {
        static message_peek = 2, static message_out_of_band = 1, static message_do_not_route = 4, static message_end_of_record = 128, static max_connections = 128}, &lt;No data fields&gt;}, &lt;No data fields&gt;}
  (gdb) p m_socket.is_open()
  $2 = true
  (gdb) bt
  #0  MaNGOS::Socket::Open (this=0x7ffff0003c40) at /home/ubuntu/MoltenCore/MoltenCore/src/shared/Network/Socket.cpp:52
  #1  0x00000000004fbff1 in MaNGOS::Listener&lt;AuthSocket&gt;::OnAccept (this=0x7fffffffe6c0, worker=0x8041e0, socket=std::shared_ptr (count 2, weak 1) 0x7ffff0003c40, ec=...) at /home/ubuntu/MoltenCore/MoltenCore/src/shared/Network/Listener.hpp:121
  #2  0x00000000004fa030 in MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}::operator()(boost::system::error_code const&amp;) const (__closure=0x7ffff4ec8be0, ec=...)
      at /home/ubuntu/MoltenCore/MoltenCore/src/shared/Network/Listener.hpp:110
  #3  0x0000000000502a2d in boost::asio::detail::binder1&lt;MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}, boost::system::error_code&gt;::operator()() (this=0x7ffff4ec8be0) at /usr/include/boost/asio/detail/bind_handler.hpp:47
  #4  0x00000000005020c3 in boost::asio::asio_handler_invoke&lt;boost::asio::detail::binder1&lt;MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}, boost::system::error_code&gt; &gt;(boost::asio::detail::binder1&lt;MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}, boost::system::error_code&gt;&amp;, ...) (function=...) at /usr/include/boost/asio/handler_invoke_hook.hpp:69
  #5  0x00000000005014b8 in boost_asio_handler_invoke_helpers::invoke&lt;boost::asio::detail::binder1&lt;MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}, boost::system::error_code&gt;, {lambda(boost::system::error_code const&amp;)#1}&gt;(boost::asio::detail::binder1&lt;MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}, boost::system::error_code&gt;&amp;, {lambda(boost::system::error_code const&amp;)#1}&amp;) (function=..., context=...)
      at /usr/include/boost/asio/detail/handler_invoke_helpers.hpp:37
  #6  0x0000000000500886 in boost::asio::detail::reactive_socket_accept_op&lt;boost::asio::basic_socket&lt;boost::asio::ip::tcp, boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;, boost::asio::ip::tcp, MaNGOS::Listener&lt;AuthSocket&gt;::BeginAccept()::{lambda(boost::system::error_code const&amp;)#1}&gt;::do_complete(boost::asio::detail::task_io_service*, boost::asio::detail::task_io_service_operation*, boost::system::error_code const&amp;, unsigned long) (owner=0x8084d0, base=0x804910) at /usr/include/boost/asio/detail/reactive_socket_accept_op.hpp:123
  #7  0x00000000004f0300 in boost::asio::detail::task_io_service_operation::complete (this=0x804910, owner=..., ec=..., bytes_transferred=0) at /usr/include/boost/asio/detail/task_io_service_operation.hpp:38
  #8  0x00000000004f1d55 in boost::asio::detail::epoll_reactor::descriptor_state::do_complete (owner=0x8084d0, base=0x804140, ec=..., bytes_transferred=1) at /usr/include/boost/asio/detail/impl/epoll_reactor.ipp:651
  #9  0x00000000004f0300 in boost::asio::detail::task_io_service_operation::complete (this=0x804140, owner=..., ec=..., bytes_transferred=1) at /usr/include/boost/asio/detail/task_io_service_operation.hpp:38
  #10 0x00000000004f2823 in boost::asio::detail::task_io_service::do_run_one (this=0x8084d0, lock=..., this_thread=..., ec=...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:372
  #11 0x00000000004f23a1 in boost::asio::detail::task_io_service::run (this=0x8084d0, ec=...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:149
  #12 0x00000000004f2abc in boost::asio::io_service::run (this=0x7ef540) at /usr/include/boost/asio/impl/io_service.ipp:59
  #13 0x00000000004f7c89 in MaNGOS::Listener&lt;AuthSocket&gt;::Listener(int, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, int)::{lambda()#1}::operator()() const () at /home/ubuntu/MoltenCore/MoltenCore/src/shared/Network/Listener.hpp:84
  #14 0x0000000000505624 in std::_Bind_simple&lt;MaNGOS::Listener&lt;AuthSocket&gt;::Listener(int, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, int)::{lambda()#1} ()&gt;::_M_invoke&lt;&gt;(std::_Index_tuple&lt;&gt;) (this=0x8049c8)
      at /usr/include/c++/5/functional:1531
  #15 0x0000000000504f38 in std::_Bind_simple&lt;MaNGOS::Listener&lt;AuthSocket&gt;::Listener(int, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, int)::{lambda()#1} ()&gt;::operator()() (this=0x8049c8) at /usr/include/c++/5/functional:1520
  #16 0x0000000000504416 in std::thread::_Impl&lt;std::_Bind_simple&lt;MaNGOS::Listener&lt;AuthSocket&gt;::Listener(int, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, int)::{lambda()#1} ()&gt; &gt;::_M_run() (this=0x8049b0) at /usr/include/c++/5/thread:115
  #17 0x00007ffff6c98c80 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
  #18 0x00007ffff7bc16ba in start_thread (arg=0x7ffff4ec9700) at pthread_create.c:333
  #19 0x00007ffff63fe82d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  </code></pre>
  
  <p>EDIT: 
  NetworkThread.hpp:</p>
  
  <pre><code>template &lt;typename SocketType&gt;
  class NetworkThread
  {
      private:
          boost::asio::io_service m_service;
  
          std::mutex m_socketLock;
          std::unordered_set&lt;std::shared_ptr&lt;SocketType&gt;&gt; m_sockets;
  
          // note that the work member *must* be declared after the service member for the work constructor to function correctly
          std::unique_ptr&lt;boost::asio::io_service::work&gt; m_work;
  
          std::thread m_serviceThread;
  
      public:
          NetworkThread() : m_work(new boost::asio::io_service::work(m_service))
          {
              m_serviceThread = std::thread([this] { boost::system::error_code ec; this-&gt;m_service.run(ec); });
          }
  
          ~NetworkThread()
          {
              // Allow io_service::run() to exit.
              m_work.reset();
              m_service.stop();
              m_serviceThread.join();
  
              // attempt to gracefully close any open connections
              for (auto i = m_sockets.begin(); i != m_sockets.end();)
              {
                  auto const current = i;
                  ++i;
  
                  if (!(*current)-&gt;IsClosed())
                      (*current)-&gt;Close();
              }
          }
  
          size_t Size() const { return m_sockets.size(); }
  
          std::shared_ptr&lt;SocketType&gt; CreateSocket();
  
          void RemoveSocket(Socket *socket)
          {
              std::lock_guard&lt;std::mutex&gt; guard(m_socketLock);
              m_sockets.erase(socket-&gt;shared&lt;SocketType&gt;());
          }
  };
  
  template &lt;typename SocketType&gt;
  std::shared_ptr&lt;SocketType&gt; NetworkThread&lt;SocketType&gt;::CreateSocket()
  {
      std::lock_guard&lt;std::mutex&gt; guard(m_socketLock);
  
      auto const i = m_sockets.emplace(std::make_shared&lt;SocketType&gt;(m_service, [this] (Socket *socket) { this-&gt;RemoveSocket(socket); }));
  
      return *i.first;
  }
  </code></pre>