private Response NameListOperation(NetworkStream dataStream, string pathname)
        {
            StreamWriter dataWriter = new StreamWriter(dataStream, _currentEncoding);

            IEnumerable<string> files = Directory.EnumerateFiles(pathname);

            foreach (string file in files)
            {
                dataWriter.WriteLine(Path.GetFileName(file));
                dataWriter.Flush();
            }

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date = DateTime.Now,
                CIP = ClientIP,
                CSMethod = "NLST",
                CSUsername = _username,
                SCStatus = "226"
            };

            _log.Info(logEntry);

            return GetResponse(FtpResponses.TRANSFER_SUCCESSFUL);
        }
        private Response ListOperation(NetworkStream dataStream, string pathnametmp)
        {
            DateTime now = DateTime.Now;
               string pathname;
               IEnumerable<string> directories;
            StreamWriter dataWriter = new StreamWriter(dataStream, _currentEncoding);
            if (pathnametmp.Contains("-"))
            {
              string[] strparam=pathnametmp.Split ('-');
              pathname = strparam[0];
            }
            else
            {
                pathname=pathnametmp;

            }
            directories = Directory.EnumerateDirectories(pathname);
            foreach (string dir in directories)
            {
                DateTime editDate = Directory.GetLastWriteTime(dir);

                string date = editDate < now.Subtract(TimeSpan.FromDays(180)) ?
                    editDate.ToString("MMM dd  yyyy", CultureInfo.InvariantCulture) :
                    editDate.ToString("MMM dd HH:mm", CultureInfo.InvariantCulture);

                dataWriter.Write("drwxr-xr-x    2 2003     2003         4096 ");
                dataWriter.Write(date);
                dataWriter.Write(' ');
                dataWriter.WriteLine(Path.GetFileName(dir));

                dataWriter.Flush();
            }

            IEnumerable<string> files = Directory.EnumerateFiles(pathname);

            foreach (string file in files)
            {
                FileInfo f = new FileInfo(file);

                string date = f.LastWriteTime < now.Subtract(TimeSpan.FromDays(180)) ?
                    f.LastWriteTime.ToString("MMM dd  yyyy", CultureInfo.InvariantCulture) :
                    f.LastWriteTime.ToString("MMM dd HH:mm", CultureInfo.InvariantCulture);

                dataWriter.Write("-rw-r--r--    2 2003     2003     ");

                string length = f.Length.ToString(CultureInfo.InvariantCulture);

                if (length.Length < 8)
                {
                    for (int i = 0; i < 8 - length.Length; i++)
                    {
                        dataWriter.Write(' ');
                    }
                }

                dataWriter.Write(length);
                dataWriter.Write(' ');
                dataWriter.Write(date);
                dataWriter.Write(' ');
                dataWriter.WriteLine(f.Name);

                dataWriter.Flush();

                f = null;
            }

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date = now,
                CIP = ClientIP,
                CSMethod = "LIST",
                CSUsername = _username,
                SCStatus = "226"
            };

            _log.Info(logEntry);

            return GetResponse(FtpResponses.TRANSFER_SUCCESSFUL);
        }
        protected override Response HandleCommand(Command cmd)
        {
            Response response = null;

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date = DateTime.Now,
                CIP = ClientIP,
                CSUriStem = cmd.RawArguments
            };

            if (!_validCommands.Contains(cmd.Code))
            {
                response = CheckUser();
            }

            // Reset rename from if we don't receive a rename to command. These must be issued back-to-back.
            if (cmd.Code != "RNTO")
            {
                _renameFrom = null;
            }

            if (response == null)
            {
                switch (cmd.Code)
                {
                    case "USER":
                        response = User(cmd.Arguments.FirstOrDefault());
                        break;
                    case "PASS":
                        response = Password(cmd.Arguments.FirstOrDefault());
                        logEntry.CSUriStem = "******";
                        break;
                    case "CWD":
                        response = ChangeWorkingDirectory(cmd.Arguments.FirstOrDefault());
                        break;
                    case "CDUP":
                        response = ChangeWorkingDirectory("..");
                        break;
                    case "QUIT":
                        response = GetResponse(FtpResponses.QUIT);
                        break;
                    case "REIN":
                        _currentUser = null;
                        _username = null;
                        _dataClient = null;
                        _currentCulture = CultureInfo.CurrentCulture;
                        _currentEncoding = Encoding.Default;
                        ControlStreamEncoding = Encoding.Default;

                        response = GetResponse(FtpResponses.SERVICE_READY);
                        break;
                    case "PORT":
                        response = Port(cmd.RawArguments);
                        logEntry.CPort = _dataEndpoint.Port.ToString(CultureInfo.InvariantCulture);
                        break;
                    case "PASV":
                        response = Passive();
                        logEntry.SPort = ((IPEndPoint)_passiveListener.LocalEndpoint).Port.ToString(CultureInfo.InvariantCulture);
                        break;
                    case "TYPE":
                        response = Type(cmd.Arguments.FirstOrDefault(), cmd.Arguments.Skip(1).FirstOrDefault());
                        break;
                    case "STRU":
                        response = Structure(cmd.Arguments.FirstOrDefault());
                        break;
                    case "MODE":
                        response = Mode(cmd.Arguments.FirstOrDefault());
                        break;
                    case "RNFR":
                        _renameFrom = cmd.Arguments.FirstOrDefault();
                        response = GetResponse(FtpResponses.RENAME_FROM);
                        break;
                    case "RNTO":
                        response = Rename(_renameFrom, cmd.Arguments.FirstOrDefault());
                        break;
                    case "DELE":
                        response = Delete(cmd.Arguments.FirstOrDefault());
                        break;
                    case "RMD":
                        response = RemoveDir(cmd.Arguments.FirstOrDefault());
                        break;
                    case "MKD":
                        response = CreateDir(cmd.Arguments.FirstOrDefault());
                        break;
                    case "PWD":
                        response = PrintWorkingDirectory();
                        break;
                    case "RETR":
                        response = Retrieve(cmd.Arguments.FirstOrDefault());
                        logEntry.Date = DateTime.Now;
                        break;
                    case "STOR":
                        response = Store(cmd.Arguments.FirstOrDefault());
                        logEntry.Date = DateTime.Now;
                        break;
                    case "STOU":
                        response = StoreUnique();
                        logEntry.Date = DateTime.Now;
                        break;
                    case "APPE":
                        response = Append(cmd.Arguments.FirstOrDefault());
                        logEntry.Date = DateTime.Now;
                        break;
                    case "LIST":
                        string p = cmd.Arguments.FirstOrDefault();
                        if (!string.IsNullOrEmpty(p))
                        {
                            if (p.Contains("-al")) p = p.Replace("-al", string.Empty);
                            if (string.IsNullOrEmpty(p)) p = null;
                        }
                        response = List(p ?? CurrentDirectory);
                        logEntry.Date = DateTime.Now;
                        break;
                    case "SYST":
                        response = GetResponse(FtpResponses.SYSTEM);
                        break;
                    case "NOOP":
                        response = GetResponse(FtpResponses.OK);
                        break;
                    case "ACCT":
                        response = Account(cmd.Arguments.FirstOrDefault());
                        break;
                    case "ALLO":
                        response = GetResponse(FtpResponses.OK);
                        break;
                    case "NLST":
                        response = NameList(cmd.Arguments.FirstOrDefault() ?? CurrentDirectory);
                        break;
                    case "SITE":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                    case "STAT":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                    case "HELP":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                    case "SMNT":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                    case "REST":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                    case "ABOR":
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;

                    // Extensions defined by rfc 2228
                    case "AUTH":
                        response = Auth(cmd.Arguments.FirstOrDefault());
                        break;

                    // Extensions defined by rfc 2389
                    case "FEAT":
                        response = GetResponse(FtpResponses.FEATURES);
                        break;
                    case "OPTS":
                        response = Options(cmd.Arguments);
                        break;

                    // Extensions defined by rfc 3659
                    case "MDTM":
                        response = FileModificationTime(cmd.Arguments.FirstOrDefault());
                        break;
                    case "SIZE":
                        response = FileSize(cmd.Arguments.FirstOrDefault());
                        break;

                    // Extensions defined by rfc 2428
                    case "EPRT":
                        response = EPort(cmd.RawArguments);
                        logEntry.CPort = _dataEndpoint.Port.ToString(CultureInfo.InvariantCulture);
                        break;
                    case "EPSV":
                        response = EPassive();
                        logEntry.SPort = ((IPEndPoint)_passiveListener.LocalEndpoint).Port.ToString(CultureInfo.InvariantCulture);
                        break;

                    // Extensions defined by rfc 2640
                    case "LANG":
                        response = Language(cmd.Arguments.FirstOrDefault());
                        break;

                    default:
                        response = GetResponse(FtpResponses.NOT_IMPLEMENTED);
                        break;
                }
            }

            logEntry.CSMethod = cmd.Code;
            logEntry.CSUsername = _username;
            logEntry.SCStatus = response.Code;

            _log.Info(logEntry);

            return response;
        }
        private Response AppendOperation(NetworkStream dataStream, string pathname)
        {
            long bytes = 0;

            using (FileStream fs = new FileStream(pathname, FileMode.Append, FileAccess.Write, FileShare.None, BUFFER_SIZE, FileOptions.SequentialScan))
            {
                bytes = CopyStream(dataStream, fs);
            }

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date = DateTime.Now,
                CIP = ClientIP,
                CSMethod = "APPE",
                CSUsername = _username,
                SCStatus = "226",
                CSBytes = bytes.ToString(CultureInfo.InvariantCulture)
            };

            _log.Info(logEntry);

            return GetResponse(FtpResponses.TRANSFER_SUCCESSFUL);
        }
        private Response StoreOperation(NetworkStream dataStream, string pathname)
        {
            long bytes = 0;

            using (FileStream fs = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, BUFFER_SIZE, FileOptions.SequentialScan))
            {
                bytes = CopyStream(dataStream, fs, FtpPerformanceCounters.IncrementBytesReceived);
            }

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date = DateTime.Now,
                CIP = ClientIP,
                CSMethod = "STOR",
                CSUsername = _username,
                SCStatus = "226",
                CSBytes = bytes.ToString(CultureInfo.InvariantCulture)
            };

            _log.Info(logEntry);

            FtpPerformanceCounters.IncrementFilesReceived();

            return GetResponse(FtpResponses.TRANSFER_SUCCESSFUL);
        }