title: Windows phone socket programming
body: <p>I want to connect to http server using sockets on windows phone 7.5(MANGO). Code looks like this:  </p>
  
  <pre><code>public class sockets
      {
          // Buffer for incoming data
          private byte[] _receiveBuffer;
  
          public void Demo()
          {
              String host = "209.85.148.106";
              int port = 80;
          String Message = "GET / HTTP/1.1 Host: google.pl User-Agent: Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8 Accept-Language: pl,en-us;q=0.7,en;q=0.3 Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n";
  
          // create endpoint
          var ipAddress = IPAddress.Parse(host);
          var endpoint = new IPEndPoint(ipAddress, port);
  
          // convert text to send (prefix with length)
          //var message = string.Format("{0};{1}", Message.Length, Message);
          var buffer = Encoding.UTF8.GetBytes(Message);
  
          // create event args
          var args = new SocketAsyncEventArgs();
          args.RemoteEndPoint = endpoint;
          args.Completed += SocketAsyncEventArgs_Completed;
          args.SetBuffer(buffer, 0, buffer.Length);
  
          // create a new socket
          var socket = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream,
              ProtocolType.Tcp);
  
          // connect socket
          bool completesAsynchronously = socket.ConnectAsync(args);
  
          // check if the completed event will be raised.
          // if not, invoke the handler manually.
          if (!completesAsynchronously)
          {
              SocketAsyncEventArgs_Completed(args.ConnectSocket, args);
          }
      }
  
      private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
      {
          // check for errors
          if (e.SocketError != SocketError.Success)
          {
              System.Diagnostics.Debug.WriteLine(e.ToString());
              return;
          }
  
          // check what has been executed
          switch (e.LastOperation)
          {
              case SocketAsyncOperation.Connect:
                  HandleConnect(e);
                  break;
              case SocketAsyncOperation.Send:
                  HandleSend(e);
                  break;
              case SocketAsyncOperation.Receive:
                  HandleReceive(e);
                  break;
              case SocketAsyncOperation.ReceiveFrom:
                  HandleReceive(e);
                  break;
          }
      }
  
      private void HandleConnect(SocketAsyncEventArgs e)
      {
          if (e.ConnectSocket != null)
          {
              // simply start sending
              bool completesAsynchronously = e.ConnectSocket.SendAsync(e);
  
              // check if the completed event will be raised.
              // if not, invoke the handler manually.
              if (!completesAsynchronously)
              {
                  SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
              }
          }
      }
  
      private void HandleSend(SocketAsyncEventArgs e)
      {
          System.Diagnostics.Debug.WriteLine("WYSLANO");
  
          // simply start sending
          bool completesAsynchronously = e.ConnectSocket.ReceiveAsync(e);
  
          // check if the completed event will be raised.
          // if not, invoke the handler manually.
          if (!completesAsynchronously)
          {
              SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
          }
      }
  
      private void HandleReceive(SocketAsyncEventArgs e)
      {
          System.Diagnostics.Debug.WriteLine("ODEBRANO");
  
  
      }
  }
  </code></pre>
  
  <p>Some of this code is from manual etc. Its trying to connect to <a href="http://google.pl" rel="nofollow">http://google.pl</a> Is sends data but does not receive anything.</p>