title: Problem creating socket with C++ in winsock2
body: <p>I'm having the weirdest problem causing me headaches. Consider the following code:</p>
  
  <pre><code>// Create and bind socket
   std::map&lt;Connection, bool&gt; clients;
   unsigned short port=6222;
   struct sockaddr_in local_address, from_address;
   int result;
   char buffer[10000];
   SOCKET receive_socket;
   local_address.sin_family = AF_INET;
   local_address.sin_addr.s_addr = INADDR_ANY;
   local_address.sin_port = htons(port);
   receive_socket = socket(AF_INET,SOCK_DGRAM,0);
  </code></pre>
  
  <p>What's happening is receive_socket is not binding, I get SOCKET_ERROR. When I debug the program and check receive_socket, it appears to just be garbled crap. I put a breakpoint on the 'std::map' line. When I step into each line of the above code, the debug cursor jumps straight from the 'unsigned short port' line to the first 'local_address.sin' line, even though I am using step into (F11), it does not stop at struct, int, char or SOCKET lines, it jumps straight over them. </p>
  
  <p>At this point I hover my mouse over local_address, from_address, result, buffer and receive_socket. They are all full of garbled crap. Is this because I have not defined these variables yet? I've also noticed that when I reach the bottom of the above code, local_address.sin_port is set to 19992, but it should be 6222?</p>
  
  <p>Edit: here is my binding code which is failing because the if statement is true:</p>
  
  <pre><code>if(bind( receive_socket, (SOCKADDR*) &amp;local_address, sizeof(local_address)) == SOCKET_ERROR) 
      {
          closesocket(receive_socket);
          return 1;
      }
  </code></pre>