/// <summary> /// If we were not provided with a btClient on creation we need to create one /// </summary> private void ConnectSocket() { try { if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Trace("Connecting bluetooth client with " + ConnectionInfo); bool connectSuccess = true; //We now connect to our target btClient = new BluetoothClient(); //Start the connection using the async version //This allows us to choose our own connection establish timeout IAsyncResult ar = btClient.BeginConnect((ConnectionInfo.RemoteEndPoint as BluetoothEndPoint), null, null); WaitHandle connectionWait = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(NetworkComms.ConnectionEstablishTimeoutMS, false)) { btClient.Close(); connectSuccess = false; } btClient.EndConnect(ar); } finally { connectionWait.Close(); } if (!connectSuccess) throw new ConnectionSetupException("Timeout waiting for remoteEndPoint to accept bluetooth connection."); } catch (Exception ex) { CloseConnection(true, 17); throw new ConnectionSetupException("Error during bluetooth connection establish with destination (" + ConnectionInfo + "). Destination may not be listening or connect timed out. " + ex.ToString()); } }