/// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        public static void RaisePeerStateChangeEvent(PEER_STATE_EVENT EVENT, object Data)
        {
            PeerStateEventHandler EventCallBack = EventHandlersTable[EVENT];
            object sender = new DiameterAAAStack();
            object e      = Data;

            //Fire Event
            EventCallBack(sender, e);
        }
        public bool Connect()
        {
            try
            {
                StateObject state = new StateObject();

                if (stackContext.peers[0].PeerConnection == null || !(stackContext.peers[0].PeerConnection.Connected))
                {
                    StackLog.Write2TraceLog("\r\n\nTransport.SendMessage()", "Establishing the Connection");

                    Common.StackLog.Write2TraceLog("Transport::SendMessage", "Connecting remote Peer[ " + stackContext.peers[0].PeerIPAddress + ":" + stackContext.peers[0].PeerPort + "]");

                    stackContext.peers[0].PeerConnection = new TcpClient(stackContext.peers[0].PeerIPAddress, stackContext.peers[0].PeerPort);

                    stackContext.peers[0].PeerConnection.ReceiveBufferSize = MAX_TCP_BUFF_SIZE;

                    Common.StackLog.Write2TraceLog("Transport::SendMessage() ", " Connection Established..");

                    //Update Peer Connection State
                    DiameterAAAStack.UpdateConnectionState(stackContext.peers[0], PeerState.OPEN);

                    Common.StackLog.Write2TraceLog("Transport::SendMessage() ", " Connection State Changed to OPEN..");

                    //Start Data Processing Thread
                    //pProcessDataThread = new Thread(processData);
                    //pProcessDataThread.Start();
                    //Start Receive CallBack

                    tcpClient = stackContext.peers[0].PeerConnection;

                    tcpClient.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                    state.workSocket = tcpClient.Client;
                }
            }
            catch (Exception e)
            {
                Common.StackLog.Write2ErrorLog("Connect", "Error Connecting:" + e.Message + " Stack:" + e.StackTrace);
                return(false);
            }
            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="listenAddresses"></param>
        public void StartListners(List <ListenAddress> listenAddresses, ref TcpListener listner)
        {
            byte[] responseBuffer = new byte[4000];

            try
            {
                SocketAddress address = new SocketAddress(System.Net.Sockets.AddressFamily.InterNetwork);

                foreach (ListenAddress socketAddr in listenAddresses)
                {
                    Common.StackLog.Write2TraceLog("Transport::SendMessage", "Starting Listening on " + socketAddr.IPAddress.ToString() + " with Port: " + socketAddr.Port.ToString());

                    listner = new TcpListener(IPAddress.Parse(socketAddr.IPAddress), socketAddr.Port);

                    listner.Start();

                    Common.StackLog.Write2TraceLog("Transport::SendMessage", "Started Listening on " + listner.LocalEndpoint.ToString());

                    StackLog.Write2TraceLog("StartListners", "Started Listening on " + socketAddr.IPAddress + ":" + socketAddr.Port.ToString());
                }

                while (true)
                {
                    //try
                    //{
                    if (listner.Pending())
                    {
                        Common.StackLog.Write2TraceLog("Transport::StartListener", "Listening for Peer Connections ");

                        Socket socket = listner.AcceptSocket();

                        Common.StackLog.Write2TraceLog("Transport::StartListener", "Recieved Message From [" + socket + " ]");

                        //Get the Message Length
                        Array.Clear(responseBuffer, 0, responseBuffer.Length);

                        int rcvdcount = socket.Receive(responseBuffer);

                        byte[] RcvdBytes = new byte[rcvdcount];

                        Buffer.BlockCopy(responseBuffer, 0, RcvdBytes, 0, rcvdcount);
                        //
                        IPEndPoint remotePeer = socket.RemoteEndPoint as IPEndPoint;

                        RecievedMessageInfo rcvdObject = new RecievedMessageInfo()
                        {
                            data = RcvdBytes, PeerIdentity = new URI("aaa://" + remotePeer.Address + ":" + remotePeer.Port + ";transport=tcp;protocol=diameter")
                        };

                        DiameterAAAStack.RaisePeerStateChangeEvent(PEER_STATE_EVENT.Rcv_Message, RcvdBytes);

                        Array.Clear(responseBuffer, 0, responseBuffer.Length);
                    }
                    //}
                    //catch (Exception ex)
                    //{
                    //}
                }
            }
            catch (Exception exp)
            {
                //Write Log Here..
                Common.StackLog.Write2ErrorLog("Transport::StartListners()", "Error:" + exp.Message + " Stack:" + exp.StackTrace);
                //Shutdown and end connection

                //listner.Stop();

                //throw exp;
            }
            finally
            {
                listner.Stop();
            }
        }
        private void Reconnect(Object obj)
        {
            StackContext stackContext = (StackContext)obj;

            DiameterAAAStack.Reconnect(stackContext);
        }