/// <summary>
 /// Start asychronouse peer port connection listener.
 /// </summary>
 /// <param name="agent"></param>
 /// <param name="port"></param>
 public void StartListening(AgentConnector connector)
 {
     Log.Logger.Info("Port connection listener starting...");
     _listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _listenerSocket.Bind(new IPEndPoint(IPAddress.Any, listenPort));
     _listenerSocket.Listen(100);
     _listenerSocket.BeginAccept(new AsyncCallback(AcceptAsyncHandler), connector);
 }
        /// <summary>
        /// Connect with remote peer and add it to the swarm when its callback handler is called.
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public void Connect(AgentConnector connector)
        {
            IPAddress localPeerIP  = Dns.GetHostEntry("localhost").AddressList[0];
            IPAddress remotePeerIP = System.Net.IPAddress.Parse(connector.peerDetails.ip);

            connector.socket = new Socket(localPeerIP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            connector.socket.BeginConnect(new IPEndPoint(remotePeerIP, connector.peerDetails.port), new AsyncCallback(ConnectionAsyncHandler), connector);
        }
        /// <summary>
        /// Asynchronous peer connection callback. When a connection
        /// is completed try to add it to the swarm via the connector
        /// passed as a parameter.
        /// </summary>
        /// <param name="ar"></param>
        private void ConnectionAsyncHandler(IAsyncResult ar)
        {
            AgentConnector connector = (AgentConnector)ar.AsyncState;

            try
            {
                connector.socket.EndConnect(ar);
                connector.callBack(connector);
            }
            catch (Exception ex)
            {
                connector.socket?.Close();
                Log.Logger.Error("Error (Ignored): " + ex.Message);
            }
        }
        internal static int listenPort = Host.DefaultPort;      // Listener port
        /// <summary>
        /// Asynchronous remote peer connection callback. When a remote connection
        /// arrives try to add it to the swarm via the connector passed then prime
        /// the accept callback for the next peers connection attempt.
        /// </summary>
        /// <param name="ar"></param>
        private void AcceptAsyncHandler(IAsyncResult ar)
        {
            AgentConnector connector = (AgentConnector)ar.AsyncState;

            try
            {
                connector.socket = _listenerSocket.EndAccept(ar);
                connector.callBack(connector.Copy());
                _listenerSocket.BeginAccept(new AsyncCallback(AcceptAsyncHandler), connector);
            }
            catch (Exception ex)
            {
                connector.socket?.Close();
                Log.Logger.Error(ex);
                Log.Logger.Info("Port connection listener terminated.");
            }
        }