title: C++ Boost.ASIO: passing accepted TCP connection from one opened socket to another using Windows APIs ( while works with Linux APIs)?
body: <p>I was trying to learn how to re assign accepted connection using Boost.ASIO and Windows API's. found <a href="https://svn.boost.org/trac/boost/ticket/5157" rel="nofollow noreferrer">this code sample</a> added to it includes and use of namespaces so now it is compilable - just copy and paste and here you go... "The parameter is incorrect" exception at the same place code poster had it=( So here is code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  
  #ifdef _WIN32
  #include "Windows.h"
  #endif
  
  using namespace boost::asio::ip;
  using namespace std;
  
  int main(){
  int m_nPort = 12345;
  boost::asio::io_service io_service;
  tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
  
  cout &lt;&lt; "Waiting for connection..." &lt;&lt; endl;
  
  tcp::socket socket(io_service);
  acceptor.accept(socket);
  cout &lt;&lt; "connection accepted" &lt;&lt; endl;
  
  #ifdef _WIN32
  WSAPROTOCOL_INFO pi;
  WSADuplicateSocket(socket.native(), GetCurrentProcessId(), &amp;pi);
  SOCKET socketDup = WSASocket(pi.iAddressFamily/*AF_INET*/, pi.iSocketType/*SOCK_STREAM*/,
                               pi.iProtocol/*IPPROTO_TCP*/, &amp;pi, 0, 0);
  char sText[] = "I can use my duplicated socket via WinApi!\r\n";
  int nRet = send(socketDup, sText, strlen(sText), 0);
  #else
  //linux
   int socketDup = dup(socket.native()); // tested on Linux, works!
  #endif
  
  try
  {
      tcp::socket s(io_service);
      s.assign(tcp::v4(), socketDup); //this throws exception under Windows
      //I can't use my socket via boost lib
      s.send(boost::asio::buffer("Not work\r\n"));
      cout &lt;&lt; "We do not get here!=(" &lt;&lt; endl;
  }
  catch(exception &amp;e)
  {
      cerr &lt;&lt; e.what() &lt;&lt; endl; //"The parameter is incorrect" exception
  }
  cin.get();
  }
  </code></pre>
  
  <p>In general <a href="https://stackoverflow.com/questions/2966323/boostasio-and-socket-ownership/2968774#2968774">code follows this post</a> and I actually do not see what is wrong neither how to fix it. </p>
  
  <p>And it follows way how we would pass accepted TCP connection from one process to another (<a href="https://stackoverflow.com/questions/5312773/c-application-is-it-possible-to-pass-accepted-tcp-connection-from-one-process/5312969#5312969">described here</a>)</p>
  
  <p>May be <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;150523" rel="nofollow noreferrer">this "Socket inheritance on different Windows platforms" example could help</a> but I do not see how.</p>
  
  <p>Can any one please help me to find any possible workaround that problem?</p>
  
  <hr>
  
  <p><strong>Update:</strong>
  Just tested code on Linux - works perfectly, no errors.</p>
  
  <p>So what is it with windows version?</p>