title: C++ CreateIoCompletionPort on new socket
body: <p>EDIT: I am guessing the problem is I have to associate the OVERLAPPED or WSAOVERLAPPED in the container with my completion port.  Is that correct?</p>
  
  <p>I can get IO completions when someone connects to my server.  I then use CreateIoCompletionPort on the new socket, with the completionport that original was used.  But when they send me data, it does not get set off.  Although, it still gets set off if someone else connects.  My question is, why would this happen?  I also make sure CreateIoCompletionPort returns the same handle as was the original.  What gives?</p>
  
  <p>EDIT:</p>
  
  <pre><code>DWORD WINAPI worker_thread(LPVOID lpParam) {
   client_information_class *cicc = NULL;
  
   HANDLE        CompletionPort = (HANDLE)lpParam;
   ULONG_PTR     Key;
   DWORD          BytesTransfered;
   OVERLAPPED     *lpOverlapped = NULL;
   DWORD          error = NULL;
   while(1) {
        error = GetQueuedCompletionStatus(CompletionPort, &amp;BytesTransfered, (PULONG_PTR)&amp;Key, &amp;lpOverlapped, 0);
        cicc = CONTAINING_RECORD ( lpOverlapped, client_information_class, ol );
  
        if ( error == TRUE ) {
             cout &lt;&lt; endl &lt;&lt; "IO TRIGGERED" &lt;&lt; endl;
             switch ( cicc-&gt;operation ) {
                  /*#define OP_ACCEPT        0
                    #define OP_READ          1
                    #define OP_WRITE         2*/
                  case 0:{
                       if ( check_auth_progress ( cicc-&gt;client_socket , cicc-&gt;client_buff , BytesTransfered ) ) {
                            cout &lt;&lt; "Client " &lt;&lt; cicc-&gt;client_socket &lt;&lt; " connected." &lt;&lt; endl; 
  
                            client_information_class *k = NULL; 
                            SOCKADDR_STORAGE *LocalSockaddr=NULL, *RemoteSockaddr=NULL; 
                            int               LocalSockaddrLen,RemoteSockaddrLen; 
  
                            k = (client_information_class *)Key;
                            k-&gt;lpfnGetAcceptExSockaddrs( 
                                 cicc-&gt;client_buff, 
                                 cicc-&gt;client_len - ((sizeof(SOCKADDR_STORAGE) + 16) * 2), 
                                 sizeof(SOCKADDR_STORAGE) + 16, 
                                 sizeof(SOCKADDR_STORAGE) + 16, 
                                 (SOCKADDR **)&amp;cicc-&gt;LocalSockaddr, 
                                 &amp;cicc-&gt;LocalSockaddrLen, 
                                 (SOCKADDR **)&amp;cicc-&gt;RemoteSockaddr, 
                                 &amp;cicc-&gt;RemoteSockaddrLen 
                            );
  
                            client_information_class *cicc2 = NULL;
                            cicc2 = ( client_information_class *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(client_information_class) + (sizeof(BYTE) * 4096));
                            if (cicc2 == NULL) {
                                 fprintf(stderr, "Out of memory!\n");
                            }
  
                            cicc2-&gt;client_socket = cicc-&gt;client_socket;
                            cicc2-&gt;client_socketaddr_in = cicc-&gt;client_socketaddr_in;
                            cicc2-&gt;LocalSockaddr = cicc-&gt;LocalSockaddr;
                            cicc2-&gt;LocalSockaddrLen = cicc-&gt;LocalSockaddrLen;
                            cicc2-&gt;RemoteSockaddr = cicc-&gt;RemoteSockaddr;
                            cicc2-&gt;RemoteSockaddrLen = cicc-&gt;RemoteSockaddrLen;
  
                            HANDLE hrc = CreateIoCompletionPort( (HANDLE)cicc2-&gt;client_socket, CompletionPort, (ULONG_PTR)cic,  0 ); 
                            if (hrc == NULL) { 
                                 fprintf(stderr, "CompletionThread: CreateIoCompletionPort failed: %d\n", GetLastError()); 
                                 return 0; 
                            } else {
                                 fprintf(stderr, "CompletionThread: CreateIoCompletionPort: %d\n", hrc);
                            }
  
                            cic-&gt;deleteNode ( cicc-&gt;client_socket , cic );
                            cic-&gt;addNode ( cicc2 );
  
                       } else {
                            cout &lt;&lt; endl &lt;&lt; "Something Happened ... " &lt;&lt; endl;
                       }
                  }break;
  
                  case 1:{
                       if ( ParsePacket ( cicc-&gt;client_socket , data ) ) {
                            cout &lt;&lt; "Client " &lt;&lt; cicc-&gt;client_socket &lt;&lt; " connected." &lt;&lt; endl;
                       } else {
                            cout &lt;&lt; endl &lt;&lt; "Something Happened ... " &lt;&lt; endl;
                       }
                  }break;
  
                  default:{
                       cout &lt;&lt; endl &lt;&lt; "Didnt catch that operation ... " &lt;&lt; cicc-&gt;operation &lt;&lt; endl;
                  }break;
             }
  
        } else if ( error == FALSE &amp;&amp; &amp;lpOverlapped == NULL )  {
             // no packet was dequed...
             fprintf(stderr, "[error == FALSE &amp;&amp; &amp;lpOverlapped == NULL] CompletionThread: GetQueuedCompletionStatus failed: %d [0x%x]\n", GetLastError(), &amp;lpOverlapped-&gt;Internal);
        } else if ( error == FALSE &amp;&amp; &amp;lpOverlapped != NULL ) {
             if((DWORD)&amp;lpOverlapped-&gt;Internal == 0x0) {     // a timeout...
             } else {
             fprintf(stderr, "[error == FALSE &amp;&amp; &amp;lpOverlapped != NULL] CompletionThread: GetQueuedCompletionStatus failed: %d [0x%x]\n", GetLastError(), &amp;lpOverlapped-&gt;Internal);
             }
        }
   }
  
  ExitThread(0);
  return 0;
  }
  </code></pre>