title: Sendto not working Windows-Form Application
body: <p>I am working on socket programming(UDP sockets) in Windows-form application.
  I have called socket() on button click then I want send some bytes on other button click.Sendto() is returning 0 when I click the desired button but data is not received on the other end but when I call sendto() from a different thread within the same application it works fine.</p>
  
  <p>Below is function to call socket(): </p>
  
  <pre><code>int socket_create(String^* error,SOCKET *sock_fd,int port_number)
  {
      sockaddr_in my_addr;
      *sock_fd = INVALID_SOCKET;
  
      // Create a SOCKET for the server to listen for client connections
      *sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
      if (*sock_fd == INVALID_SOCKET)
      {
          this-&gt;richTextBox1-&gt;AppendText("Error at socket():"+     WSAGetLastError() + "\n");
          return -1;
      }
      else 
          this-&gt;richTextBox1-&gt;AppendText("Socket succesfully opened on port number: " + port_number + "\n");
      // The sockaddr_in structure specifies the address family,
      // IP address, and port for the socket that is being bound.
      my_addr.sin_family = AF_INET;
      my_addr.sin_addr.s_addr = inet_addr("192.168.1.2");
      my_addr.sin_port = htons(port_number);
  
      if (bind(*sock_fd, (SOCKADDR *) &amp; my_addr, sizeof (my_addr)) == SOCKET_ERROR)
      {
          this-&gt;richTextBox1-&gt;AppendText("bind failed:" + WSAGetLastError()+ "\n");
          //freeaddrinfo(result);
          closesocket(*sock_fd);
          return -1;
      }
      else
          this-&gt;richTextBox1-&gt;AppendText("Bind succesful on port number:" + port_number + "\n");
      return 0;
  }
  </code></pre>
  
  <p>Below is button code which calls socket_create() to open socket:</p>
  
  <pre><code>private: System::Void Connect_btn_Click(System::Object^  sender, System::EventArgs^  e) 
  {
      int status = 0;
      String^ error;
      status = socket_create(&amp;error, &amp;s_8888,8888);   
  }
  </code></pre>
  
  <p>socket descriptor s_8888 is defined globally.</p>
  
  <p>Below is button code to call sendto():</p>
  
  <pre><code>private: System::Void test_btn_Click(System::Object^  sender, System::EventArgs^  e) 
  {
      sockaddr_in their_addr;
      their_addr.sin_family = AF_INET;
      their_addr.sin_addr.s_addr = inet_addr(ip_address);
      their_addr.sin_port = htons(8888);
      int length = sizeof (their_addr);
      i=sendto(s_8888 , buf , sizeof(buf), 0, (SOCKADDR *) &amp;their_addr, length);
      if(i== SOCKET_ERROR)
      {
          MessageBox::Show("" + WSAGetLastError());
      }
      richTextBox1-&gt;AppendText("" + i + "\n");
  }
  </code></pre>
  
  <p>I am unable to understand why it is behaving like that and how to resolve it?</p>