public HttpConnection openConnection(string address, int port) { HttpConnection thisConnection; try { TcpClient client = new TcpClient(); client.Connect(address, port); thisConnection = new HttpConnection(address, port, address, client.GetStream()); } catch (SocketException se) { throw new NetworkException("Stream cannot be opened. Socket error " + se.ToString()); } return thisConnection; }
// in the fure HttpConnections can have some form of callback or events that will register with the entity public HttpConnection openSecureConnection(string address, int port, string servername) { HttpConnection thisConnection; try { TcpClient client = new TcpClient(address, port); RemoteCertificateValidationCallback call_bk = ValidateServerCertificate; if (this.UNSECURE) { call_bk = ValidateServerCertificate_insecure; } SslStream sslStream = new SslStream( client.GetStream(), false, new RemoteCertificateValidationCallback(call_bk), null ); // The server name must match the name on the server certificate. sslStream.AuthenticateAsClient(servername); // may throw AuthenticationException thisConnection = new HttpConnection(address, port, servername, sslStream); } catch (SocketException se) { throw new NetworkException("SSL: Stream cannot be opened. Socket error " + se.ToString()); } catch (IOException se) { throw new NetworkException("SSL: Stream cannot be opened. Socket error " + se.ToString()); } return thisConnection; }