title: Why is my socket's open port not listed by netstat?
body: <p>If you run this example, you'll see the port is never listed by <code>netstat</code>. Why? And how do I make it so?</p>
  
  <pre><code>#include &lt;WinSock.h&gt;
  #include &lt;io.h&gt;
  #include &lt;stdio.h&gt;
  
  #pragma comment(lib, "WS2_32")
  
  int main() {
      WORD wVers = MAKEWORD(2, 2);
      WSADATA wsa;
      WSAStartup(wVers, &amp;wsa);
      SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
      if (sock != INVALID_SOCKET) {
          struct sockaddr_in addr = { 0 };
          addr.sin_family = AF_INET;
          addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
          int addrlen = sizeof(addr);
          bind(sock, (struct sockaddr *)&amp;addr, addrlen);
          if (getsockname(sock, (struct sockaddr *)&amp;addr, &amp;addrlen) == 0) {
              fprintf(stdout, "HANDLE = %d, port = %d\n", sock, addr.sin_port);
              fflush(stdout);
              system("netstat -a -n");
          }
          closesocket(sock);
      }
      WSACleanup();
  }
  </code></pre>