public void Close()
        {
            lock (_InitializationLock)
            {
                try
                {
                    if (null != _Socket)
                    {
                        _Socket.Close();
                        _Socket = null;
                    }

                    if (null != _IncomingStreamHandler)
                    {
                        _IncomingStreamHandler.Stop();
                        _IncomingStreamHandler = null;
                    }

                    if (null != _OutgoingStreamHandler)
                    {
                        _OutgoingStreamHandler.Stop();
                        _OutgoingStreamHandler = null;
                    }

                    //notify connection change
                    if (null != _ConnectionListener)
                    {
                        _ConnectionListener.OnGazeApiConnectionStateChanged(false);
                    }

                    //We cancel queued requests
                    if (null != _RequestQueue)
                    {
                        CancelAllRequests();
                        _RequestQueue.Stop();
                    }
                    _RequestQueue = null;

                    //We make sure we cancel currently ongoing requests
                    if (null != _OngoingRequests)
                    {
                        IEnumerator <KeyValuePair <int, IRequest> > reqs = _OngoingRequests.GetEnumerator();

                        while (reqs.MoveNext())
                        {
                            reqs.Current.Value.Cancel();
                        }
                        _OngoingRequests.Clear();
                    }
                    _OngoingRequests = null;
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error closing socket: " + e.Message);
                }
            }
        }
        public bool Connect(string host, int port, long timeout)
        {
            lock (_InitializationLock)
            {
                if (IsConnected())
                {
                    Close();
                }

                try
                {
                    _Socket = new TcpClient();

                    _RequestQueue = new PriorityBlockingQueue <IRequest>();
                    _RequestQueue.Start();
                    _OngoingRequests = new ConcurrentDictionary <int, IRequest>();

                    IAsyncResult result    = _Socket.BeginConnect(host, port, null, null);
                    bool         connected = result.AsyncWaitHandle.WaitOne((int)timeout);

                    if (connected)
                    {
                        _Socket.EndConnect(result);
                        _Socket.ReceiveTimeout = (int)timeout;

                        //notify connection change
                        if (null != _ConnectionListener)
                        {
                            _ConnectionListener.OnGazeApiConnectionStateChanged(_Socket.Connected);
                        }

                        _IncomingStreamHandler = new IncomingStreamHandler(_Socket, _ResponseListener, _ConnectionListener, _OngoingRequests, this);
                        _IncomingStreamHandler.Start();

                        _OutgoingStreamHandler = new OutgoingStreamHandler(_Socket, _RequestQueue, _OngoingRequests, _ConnectionListener, this);
                        _OutgoingStreamHandler.Start();

                        return(true);
                    }
                }
                catch (SocketException se)
                {
                    Debug.WriteLine("Unable to open socket. Is Tracker Server running? Exception: " + se.Message);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception while establishing socket connection. Is Tracker Server running? Exception: " + e.Message);
                }

                Close();

                return(false);
            }
        }
        public bool Connect(string host, int port, long timeout)
        {
            lock (_InitializationLock)
            {
                if (IsConnected())
                    Close();

                try
                {
                    _Socket = new TcpClient();

                    _RequestQueue = new PriorityBlockingQueue<IRequest>();
                    _RequestQueue.Start();
                    _OngoingRequests = new ConcurrentDictionary<int, IRequest>();

                    IAsyncResult result = _Socket.BeginConnect(host, port, null, null);
                    bool connected = result.AsyncWaitHandle.WaitOne((int)timeout);

                    if (connected)
                    {
                        _Socket.EndConnect(result);
                        _Socket.ReceiveTimeout = (int)timeout;

                        //notify connection change
                        if (null != _ConnectionListener)
                            _ConnectionListener.OnGazeApiConnectionStateChanged(_Socket.Connected);

                        _IncomingStreamHandler = new IncomingStreamHandler(_Socket, _ResponseListener, _ConnectionListener, _OngoingRequests, this);
                        _IncomingStreamHandler.Start();

                        _OutgoingStreamHandler = new OutgoingStreamHandler(_Socket, _RequestQueue, _OngoingRequests, _ConnectionListener, this);
                        _OutgoingStreamHandler.Start();

                        return true;
                    }
                }
                catch (SocketException se)
                {
                    Debug.WriteLine("Unable to open socket. Is Tracker Server running? Exception: " + se.Message);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception while establishing socket connection. Is Tracker Server running? Exception: " + e.Message);
                }

                Close();

                return false;
            }
        }
        public void Close()
        {
            lock (_InitializationLock)
            {
                try
                {
                    if (null != _Socket)
                    {
                        _Socket.Close();
                        _Socket = null;
                    }

                    if (null != _IncomingStreamHandler)
                    {
                        _IncomingStreamHandler.Stop();
                        _IncomingStreamHandler = null;
                    }

                    if (null != _OutgoingStreamHandler)
                    {
                        _OutgoingStreamHandler.Stop();
                        _OutgoingStreamHandler = null;
                    }

                    //notify connection change
                    if (null != _ConnectionListener)
                        _ConnectionListener.OnGazeApiConnectionStateChanged(false);

                    //We cancel queued requests
                    if (null != _RequestQueue)
                    {
                        CancelAllRequests();
                        _RequestQueue.Stop();
                    }
                    _RequestQueue = null;

                    //We make sure we cancel currently ongoing requests
                    if (null != _OngoingRequests)
                    {
                        IEnumerator<KeyValuePair<int, IRequest>> reqs = _OngoingRequests.GetEnumerator();

                        while(reqs.MoveNext())
                        {
                            reqs.Current.Value.Cancel();
                        }
                        _OngoingRequests.Clear();
                    }
                    _OngoingRequests = null;

                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error closing socket: " + e.Message);
                }
            }
        }