protected virtual void OnStarted()
 {
     connection = DoConnect(SourceUri);
     if (connection == null)
     {
         DoStop(StopReason.ConnectionError);
     }
 }
 protected StreamConnection DoConnect(IPEndPoint endpoint)
 {
     try {
     client = new TcpClient();
     client.Connect(endpoint);
     remoteEndPoint = (IPEndPoint)client.Client.RemoteEndPoint;
     var stream = client.GetStream();
     var connection = new StreamConnection(stream, stream);
     connection.ReceiveTimeout = 10000;
     connection.SendTimeout    = 8000;
     Logger.Debug("Connected: {0}", endpoint);
     return connection;
       }
       catch (SocketException e) {
     Logger.Debug("Connection Failed: {0}", endpoint);
     Logger.Debug(e);
     return null;
       }
 }
 protected override void DoClose(StreamConnection connection)
 {
     Logger.Debug("closing connection");
       connection.Close();
       Logger.Debug("closing client");
       client.Close();
       Logger.Debug("closed");
       state = State.Disconnected;
 }
 protected override StreamConnection DoConnect(Uri source)
 {
     try {
     client = new TcpClient();
     if (source.HostNameType==UriHostNameType.IPv4 ||
     source.HostNameType==UriHostNameType.IPv6) {
       client.Connect(IPAddress.Parse(source.Host), source.Port);
     }
     else {
       client.Connect(source.DnsSafeHost, source.Port);
     }
     var stream = client.GetStream();
     var connection = new StreamConnection(stream, stream);
     connection.ReceiveTimeout = 10000;
     connection.SendTimeout    = 8000;
     return connection;
       }
       catch (SocketException e) {
     Logger.Error(e);
     return null;
       }
 }
 protected override void DoClose(StreamConnection connection)
 {
     connection.Close();
       client.Close();
       state = State.Disconnected;
 }
 protected override StreamConnection DoConnect(Uri source)
 {
     try {
     client = new TcpClient();
     client.Connect(source.DnsSafeHost, source.Port);
     var stream = client.GetStream();
     var connection = new StreamConnection(stream, stream);
     connection.ReceiveTimeout = 3000;
     connection.SendTimeout    = 3000;
     return connection;
       }
       catch (SocketException e) {
     Logger.Error(e);
     return null;
       }
 }
 protected virtual void OnStarted()
 {
     connection = DoConnect(SourceUri);
       if (connection==null) {
     DoStop(StopReason.ConnectionError);
       }
 }
 protected abstract void DoClose(StreamConnection connection);
 protected abstract void             DoClose(StreamConnection connection);
 protected override void DoClose(StreamConnection connection)
 {
     this.connection.Close();
       this.client.Close();
       Logger.Debug("Closed");
 }