title: TCP sockets transfer from windows phone 8
body: <p>i am doing a sample in which I am trying to transfer data from client(windows phone 8) app to  windows form app(which acts as a server) using TCP.</p>
  
  <p>In windows form app I am creating a listener like this</p>
  
  <pre><code>         TcpListener tcpListener = null;
  
          IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
          try
          {
              // Set the listener on the local IP address
              // and specifing the port.
              tcpListener = new TcpListener(ipAddress, 13);
              tcpListener.Start();
              output = "Waiting for a connection...";
          }
          catch (Exception e)
          {
              output = "Error: " + e.ToString();
              MessageBox.Show(output);
          }
  
          while (true)
          {
  
              Thread.Sleep(10);
               TcpClient tcpClient = tcpListener.AcceptTcpClient();
  
              byte[] bytes = new byte[256];
  
              NetworkStream stream = tcpClient.GetStream();
              stream.Read(bytes, 0, bytes.Length);
            }
  </code></pre>
  
  <p>In windows phone 8 app i am using managed sockets to connect to the listener some thing like this</p>
  
  <pre><code>    public Task SendUsingManagedSocketsAsync(string strServerIP)
      {
  
  
          // enable asynchronous task completion
          completionSource = new TaskCompletionSource&lt;object&gt;();
          // create a new socket 
          var socket = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream,
              ProtocolType.Tcp);
  
          // create endpoint 
          var ipAddress = IPAddress.Parse(strServerIP);
          var endpoint = new IPEndPoint(ipAddress, PORT);
  
          // create event args 
          var args = new SocketAsyncEventArgs();
          args.RemoteEndPoint = endpoint;
          args.Completed += SocketConnectCompleted;
  
          // check if the completed event will be raised. If not, invoke the handler manually. 
          if (!socket.ConnectAsync(args))
              SocketConnectCompleted(args.ConnectSocket, args);
  
          return completionSource.Task;
      }
  </code></pre>
  
  <p>But i got an socket error of connection refused. Is my approach is correct, how to connect a tcp listener through windows phone.. Here i cannot change the code of windows form app as it was my server code in the real time.. Or is it compulsary to change the code of my server.... </p>