public USSDSessionManager()
        {
            running        = true;
            sessionTimeout = ConfigurationAppSettingsWrapper <int> .GetValue("Session Timeout", 180000);

            timeoutThread = new Thread(new ThreadStart(CleanupSessions));
            timeoutThread.Start();
        }
        protected void OnConnectedToServer(IAsyncResult asyncResult)
        {
            if (tcpClientSocket == null)
            {
                return;
            }
            try
            {
                tcpClientSocket.EndConnect(asyncResult);
                LogManager.LogStatus("{0} connected to {1}{2} ", name, address, (port == null ? String.Empty : ":" + port.Value.ToString()));
                if (tcpClientSocket.Connected)
                {
                    listenerState = ListenerStates.Running;
                    if (listenerState == ListenerStates.Running)
                    {
                        OnConnectedToServer(tcpClientSocket);
                    }
                }
                else
                {
                    ConnectToServer(true);
                }
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.TimedOut && (listenerState != ListenerStates.Stopping || listenerState != ListenerStates.Stopped))
                {
                    int retryInterval = ConfigurationAppSettingsWrapper <int> .GetValue("Tcp Connection retry interval");

                    LogManager.LogError("Timed out connecting to {0}. Retrying", RemoteIPEndPoint.ToString());
                    connectWaitEvent.Reset();
                    connectWaitEvent.WaitOne(new TimeSpan(0, 0, 0, 0, retryInterval));
                    try
                    {
                        if (tcpClientSocket != null)
                        {
                            tcpClientSocket.BeginConnect(RemoteIPEndPoint, new AsyncCallback(OnConnectedToServer), tcpClientSocket);
                        }
                    }
                    catch (Exception)
                    {
                        if (listenerState != ListenerStates.Stopping || listenerState != ListenerStates.Stopped)
                        {
                            throw;
                        }
                    }
                }
                else if (se.SocketErrorCode == SocketError.ConnectionRefused && (listenerState != ListenerStates.Stopping || listenerState != ListenerStates.Stopped))
                {
                    int retryInterval = ConfigurationAppSettingsWrapper <int> .GetValue("Tcp Connection retry interval");

                    LogManager.LogError("Unable to connect to {0}. Retrying", RemoteIPEndPoint.ToString());
                    connectWaitEvent.Reset();
                    connectWaitEvent.WaitOne(new TimeSpan(0, 0, 0, 0, retryInterval));
                    try
                    {
                        if (tcpClientSocket != null)
                        {
                            tcpClientSocket.BeginConnect(RemoteIPEndPoint, new AsyncCallback(OnConnectedToServer), tcpClientSocket);
                        }
                    }
                    catch (Exception)
                    {
                        if (listenerState != ListenerStates.Stopping || listenerState != ListenerStates.Stopped)
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    LogManager.LogError(se);
                }
            }
        }