public void Connect( string ip, int port )
{
Disconnect( );
lock ( sync )
{
try
{
// make sure communication queue is empty
communicationQueue.Clear( );
endPoint = new IPEndPoint( IPAddress.Parse( ip ), Convert.ToInt16( port ) );
// create TCP/IP socket and set timeouts
socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
socket.ReceiveTimeout = 5000;
socket.SendTimeout = 1000;
// connect to SVS
socket.Connect( endPoint );
// create events
stopEvent = new ManualResetEvent( false );
requestIsAvailable = new AutoResetEvent( false );
replyIsAvailable = new AutoResetEvent( false );
// create and start new thread
thread = new Thread( new ThreadStart( CommunicationThread ) );
thread.Start( );
}
catch ( SocketException )
{
socket.Close( );
socket = null;
endPoint = null;
throw new ConnectionFailedException( "Failed connecting to SRV-1." );
}
}
}