title: How to force client in UDP to open port when sending with sendto
body: <p>I have simple server and client in UDP (WinSocks/C++).</p>
  
  <p>I send datagram client -> server via sendto, and reply from server to client using the ip and port obtained from recvfrom function.</p>
  
  <p>I found out that:</p>
  
  <ol>
  <li>Every sendto from client is being sent from different port</li>
  <li>When trying to reply from server Windows returns WSAECONNRESET (which mean that port is closed - <a href="http://support.microsoft.com/kb/263823" rel="nofollow">http://support.microsoft.com/kb/263823</a>)</li>
  </ol>
  
  <p>How can I properly answer client from server (ie force port binding on client when sending using sendto?)</p>
  
  <p>Edit: Adding some source code:</p>
  
  <pre><code>bool InitClient()
  {
      internal-&gt;sock = socket(PF_INET, SOCK_DGRAM, 0);
      char8 yes = 1;
      setsockopt(internal-&gt;sock, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int32));
      return internal-&gt;sock != -1;
  }
  void Send(const IpAddress &amp; target, const uint16 port, const char8 * data, int32 size )
  {
      sockaddr_in trgt;
      memset(&amp;trgt, 0, sizeof(trgt));
      trgt.sin_family = AF_INET;
      trgt.sin_port = htons(port);
      trgt.sin_addr.s_addr = target.GetRaw();
  
      if(sendto(internal-&gt;sock, (const char8 *)data, size, 0, (PSOCKADDR)&amp;trgt, sizeof(trgt)) == SOCKET_ERROR)
      {
          LOG("Network sending error: %d", WSAGetLastError());
      }
  }
  </code></pre>