title: Can't make TCP client code work without USB cable attached
body: <p>I'm having a bit of trouble in getting a very simple TCP client working on my HTC Titan w/ Windows Phone 7.5. </p>
  
  <p>When the USB cable is attached to the phone, the TCP client works like a charm, but as soon as the cable is unplugged, the client is unable to connect to a TCP server running on my development machine. The devices are on the same network and I'm using the explicit IP-address of my desktop machine to connect, so there's no name resolution going on afaik.</p>
  
  <p>Here's the code I use. Most of it was taken from the Sockets samples on MSDN (can't seem to find the link now though).</p>
  
  <pre><code>
  private Socket _sock = null;
  private ManualResetEvent _done = new ManualResetEvent(false);
  private const int TIMEOUT = 5000;
  
  //connect to server
  public string Connect(string ip, int port) {
      string result = string.Empty;
      var host = new IPEndpoint(IPAddress.Parse(ip), port);
      _sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      _sock.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular);
  
      var args = new SocketAsyncEventArgs();
      args.RemoteEndPoint = host;
      args.Completed += new EventHandler((s,e) => {
          result = e.SocketError.ToString();
          _done.Set();
      });
  
      _done.Reset();
      _sock.ConnectAsync(args);
      _done.WaitOne(TIMEOUT);
      return result;
  }
  
  //send message
  public string Send(string msg) {
      string response = "Operation timeout";
      if (_sock != null) {
          var args= new SocketAsyncEventArgs();
          args.RemoteEndPoint = _sock.RemoteEndPoint;
          args.Completed += new EventHandler(s, e) => {
              response = e.SocketError.ToString();
              _done.Set();
          });
  
          var payload = Encoding.UTF8.GetBytes(data);
          args.SetBuffer(payload, 0, payload.Length);
  
          _done.Reset();
          _sock.SendAsync(args);
          _done.WaitOne(TIMEOUT);
      }
  
      return response;
  }
  
  //receive message
  public string Receive() {
      string response = "Operation timeout";
  
      if (_sock != null) {
          var args= new SocketAsyncEventArgs();
          args.RemoteEndPoint = _sock.RemoteEndPoint;
          args.SetBuffer(new Byte[MAX_BUFSIZE], 0, MAX_BUFSIZE);
          args.Completed += new EventHandler((s,e) => {
              if (e.SocketError == SocketError.Success) {
                  response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                  response = response.Trim('\0');
              }
              else {
                  response = e.SocketError.ToString();
              }
  
              _done.Set();
      });
  
      _done.Reset();
      _sock.ReceiveAsync(args);
      _done.WaitOne(TIMEOUT);
      }
  
      return response;
  }
  </code></pre>
  
  <p>The code is then simply used like:</p>
  
  <pre><code>
  Connect(...);
  Send(...);
  Receive(...);
  //and then close the socket
  </code></pre>
  
  <p>As I said before, the code works like a charm when the device is attached to my development machine. When the cable is unplugged, the connection phase just times out (regardless of the timeout interval I should say).</p>
  
  <p>Also, the manifest contains the ID_CAP_NETWORKING capability which as I understand it should give the app permission to access the network.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT:</strong>
  I discovered that switching to UDP communication works like a charm. Which means that the problem is that for some reason, the phone is unable to set up a persistant TCP connection to my dev machine. This is getting stranger by the minute.</p>