/// <summary> /// Starts an asynchronous connect operation to connect to a remote server. /// </summary> /// <param name="remoteAddress">The remote address to connect to.</param> /// <param name="remotePort">The remote port to use during the connection attempt.</param> public void ConnectAsync(string remoteAddress, int remotePort) { var remoteEndPoint = EndPointHelper.ParseEndPoint(remoteAddress, remotePort); _logger.Trace("Connecting to remote endpoint {0}", remoteEndPoint); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); #if WINDOWS_PHONE // make sure we only use the socket when we have a non-cellular (Wi-Fi or similar) connection socket.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular); #endif // update operation arguments _socketOperation.RemoteEndPoint = remoteEndPoint; // connect socket bool completesAsynchronously = socket.ConnectAsync(_socketOperation); // check if the completed event will be raised. // if not, invoke the handler manually. if (!completesAsynchronously) { SocketAsyncEventArgs_Completed(_socketOperation.ConnectSocket, _socketOperation); } }