public void Start()
        {
            if (!_socket.Connected)
            {
                return;
            }

            _reader = new StreamReader(_socket.Stream);
            _writer = new StreamWriter(_socket.Stream)
            {
                AutoFlush = true
            };

            _writer.WriteLine("220 localhost Server Ready");
            var isMessageBody = false;

            while (_socket.Connected)
            {
                var token = SmtpToken.FromLine(_reader.ReadLine(), isMessageBody);

                SmtpLog.Debug(token.Data);

                var handler = ProtocolHandlers.HandlerFor(token);
                if (handler.Handle(token, this) == ContinueProcessing.Stop)
                {
                    break;
                }

                isMessageBody = token.IsData && token.IsMessageBody;
            }
        }
        private void handleException(Exception exc)
        {
            if (exc is AggregateException)
            {
                var inner = exc.InnerException;
                if (inner != null)
                {
                    handleException(exc);
                }
                return;
            }

            if (exc is ObjectDisposedException)
            {
                return;
            }

            SmtpLog.Error("Listener socket is closed", exc);
        }
        public void OnClientConnect(ISocket clientSocket)
        {
            if (_closed)
            {
                return;
            }

            SmtpLog.Info("Client connected");
            ListenForClients();

            var session = new SmtpSession(clientSocket)
            {
                OnMessage = (msg) => _messages.Add(msg)
            };

            session.Start();

            _sessions.Add(session);
        }
 public void WriteResponse(string data)
 {
     SmtpLog.Debug(data);
     _writer.WriteLine(data);
 }
 public void Start()
 {
     Listener.Start();
     SmtpLog.Info(string.Format("Server started at {0}", new IPEndPoint(Address, Port)));
     ListenForClients();
 }