protected virtual void SetupData()
        {
            Random = new Random();

            _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
            ConnectionInfo  = new ConnectionInfo(
                _serverEndPoint.Address.ToString(),
                _serverEndPoint.Port,
                "user",
                new PasswordAuthenticationMethod("user", "password"))
            {
                Timeout = TimeSpan.FromSeconds(20)
            };
            _keyExchangeAlgorithm = Random.Next().ToString(CultureInfo.InvariantCulture);
            SessionId             = new byte[10];
            Random.NextBytes(SessionId);
            DisconnectedRegister        = new List <EventArgs>();
            DisconnectReceivedRegister  = new List <MessageEventArgs <DisconnectMessage> >();
            ErrorOccurredRegister       = new List <ExceptionEventArgs>();
            ServerBytesReceivedRegister = new List <byte[]>();

            Session = new Session(ConnectionInfo, _serviceFactoryMock.Object);
            Session.Disconnected            += (sender, args) => DisconnectedRegister.Add(args);
            Session.DisconnectReceived      += (sender, args) => DisconnectReceivedRegister.Add(args);
            Session.ErrorOccured            += (sender, args) => ErrorOccurredRegister.Add(args);
            Session.KeyExchangeInitReceived += (sender, args) =>
            {
                var newKeysMessage = new NewKeysMessage();
                var newKeys        = newKeysMessage.GetPacket(8, null);
                ServerSocket.Send(newKeys, 4, newKeys.Length - 4, SocketFlags.None);
            };

            ServerListener            = new AsyncSocketListener(_serverEndPoint);
            ServerListener.Connected += socket =>
            {
                ServerSocket = socket;

                socket.Send(Encoding.ASCII.GetBytes("\r\n"));
                socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
                socket.Send(Encoding.ASCII.GetBytes("SSH-2.0-SshStub\r\n"));
            };

            var counter = 0;

            ServerListener.BytesReceived += (received, socket) =>
            {
                ServerBytesReceivedRegister.Add(received);

                switch (counter++)
                {
                case 0:
                    var keyExchangeInitMessage = new KeyExchangeInitMessage
                    {
                        CompressionAlgorithmsClientToServer = new string[0],
                        CompressionAlgorithmsServerToClient = new string[0],
                        EncryptionAlgorithmsClientToServer  = new string[0],
                        EncryptionAlgorithmsServerToClient  = new string[0],
                        KeyExchangeAlgorithms       = new[] { _keyExchangeAlgorithm },
                        LanguagesClientToServer     = new string[0],
                        LanguagesServerToClient     = new string[0],
                        MacAlgorithmsClientToServer = new string[0],
                        MacAlgorithmsServerToClient = new string[0],
                        ServerHostKeyAlgorithms     = new string[0]
                    };
                    var keyExchangeInit = keyExchangeInitMessage.GetPacket(8, null);
                    ServerSocket.Send(keyExchangeInit, 4, keyExchangeInit.Length - 4, SocketFlags.None);
                    break;

                case 1:
                    var serviceAcceptMessage =
                        ServiceAcceptMessageBuilder.Create(ServiceName.UserAuthentication)
                        .Build();
                    ServerSocket.Send(serviceAcceptMessage, 0, serviceAcceptMessage.Length, SocketFlags.None);
                    break;
                }
            };
        }
        protected virtual void SetupData()
        {
            Random = new Random();

            _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
            ConnectionInfo  = new ConnectionInfo(
                _serverEndPoint.Address.ToString(),
                _serverEndPoint.Port,
                "user",
                new PasswordAuthenticationMethod("user", "password"))
            {
                Timeout = TimeSpan.FromSeconds(20)
            };
            _keyExchangeAlgorithm = Random.Next().ToString(CultureInfo.InvariantCulture);
            SessionId             = new byte[10];
            Random.NextBytes(SessionId);
            DisconnectedRegister        = new List <EventArgs>();
            DisconnectReceivedRegister  = new List <MessageEventArgs <DisconnectMessage> >();
            ErrorOccurredRegister       = new List <ExceptionEventArgs>();
            ServerBytesReceivedRegister = new List <byte[]>();
            ServerIdentification        = new SshIdentification("2.0", "OurServerStub");
            _authenticationStarted      = false;
            _disconnectMessage          = new DisconnectMessage(DisconnectReason.ServiceNotAvailable, "Not today!");
            _socketFactory = new SocketFactory();

            Session = new Session(ConnectionInfo, _serviceFactoryMock.Object, _socketFactoryMock.Object);
            Session.Disconnected            += (sender, args) => DisconnectedRegister.Add(args);
            Session.DisconnectReceived      += (sender, args) => DisconnectReceivedRegister.Add(args);
            Session.ErrorOccured            += (sender, args) => ErrorOccurredRegister.Add(args);
            Session.KeyExchangeInitReceived += (sender, args) =>
            {
                var newKeysMessage = new NewKeysMessage();
                var newKeys        = newKeysMessage.GetPacket(8, null);
                ServerSocket.Send(newKeys, 4, newKeys.Length - 4, SocketFlags.None);
            };

            ServerListener            = new AsyncSocketListener(_serverEndPoint);
            ServerListener.Connected += socket =>
            {
                ServerSocket = socket;

                // Since we're mocking the protocol version exchange, we'll immediately stat KEX upon
                // having established the connection instead of when the client has been identified

                var keyExchangeInitMessage = new KeyExchangeInitMessage
                {
                    CompressionAlgorithmsClientToServer = new string[0],
                    CompressionAlgorithmsServerToClient = new string[0],
                    EncryptionAlgorithmsClientToServer  = new string[0],
                    EncryptionAlgorithmsServerToClient  = new string[0],
                    KeyExchangeAlgorithms       = new[] { _keyExchangeAlgorithm },
                    LanguagesClientToServer     = new string[0],
                    LanguagesServerToClient     = new string[0],
                    MacAlgorithmsClientToServer = new string[0],
                    MacAlgorithmsServerToClient = new string[0],
                    ServerHostKeyAlgorithms     = new string[0]
                };
                var keyExchangeInit = keyExchangeInitMessage.GetPacket(8, null);
                ServerSocket.Send(keyExchangeInit, 4, keyExchangeInit.Length - 4, SocketFlags.None);
            };
            ServerListener.BytesReceived += (received, socket) =>
            {
                ServerBytesReceivedRegister.Add(received);

                if (!_authenticationStarted)
                {
                    var serviceAcceptMessage = ServiceAcceptMessageBuilder.Create(ServiceName.UserAuthentication).Build();
                    ServerSocket.Send(serviceAcceptMessage, 0, serviceAcceptMessage.Length, SocketFlags.None);
                    _authenticationStarted = true;
                }
            };

            ServerListener.Start();

            ClientSocket = new DirectConnector(_socketFactory).Connect(ConnectionInfo);
        }