/// <summary>
        /// Expect client to connect share "$IPC", tcp or netbios connect is not included
        /// </summary>
        /// <param name="timeout">timeout</param>
        /// <returns>The client endpoint</returns>
        public override FsEndpoint ExpectConnectIpcShare(TimeSpan timeout)
        {
            CifsServerPerConnection   connection;
            SmbNegotiateRequestPacket negotiateRequest = this.cifsServer.ExpectPacket(
                timeout, out connection) as SmbNegotiateRequestPacket;

            //ushort uid = (ushort)Interlocked.Increment(ref nextUid);

            SmbNegotiateResponsePacket negotiateResponse =
                this.cifsServer.CreateNegotiateResponse(connection, negotiateRequest, SecurityModes.NONE);

            this.cifsServer.SendPacket(negotiateResponse, connection);

            SmbSessionSetupAndxRequestPacket sessionSetupRequest =
                this.cifsServer.ExpectPacket(timeout, out connection) as SmbSessionSetupAndxRequestPacket;

            SmbSessionSetupAndxResponsePacket sessionSetupResponse =
                this.cifsServer.CreateSessionSetupAndxResponse(connection, sessionSetupRequest, ActionValues.NONE,
                                                               null);

            this.cifsServer.SendPacket(sessionSetupResponse, connection);

            SmbTreeConnectAndxRequestPacket treeConnectRequest =
                this.cifsServer.ExpectPacket(timeout, out connection) as SmbTreeConnectAndxRequestPacket;


            SmbTreeConnectAndxResponsePacket treeConnectResponse =
                this.cifsServer.CreateTreeConnectAndxResponse(connection, treeConnectRequest, OptionalSupport.NONE,
                                                              IPC_SERVICE_NAME, null);

            this.cifsServer.SendPacket(treeConnectResponse, connection);

            return(this.fsEndpoints[connection.Identity]);
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTreeConnectAndxRequestPacket(SmbTreeConnectAndxRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.smbParameters.WordCount = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset = packet.SmbParameters.AndXOffset;
            this.smbParameters.Flags = packet.SmbParameters.Flags;
            this.smbParameters.PasswordLength = packet.SmbParameters.PasswordLength;

            this.smbData.ByteCount = packet.SmbData.ByteCount;
            if (packet.smbData.Password != null)
            {
                this.smbData.Password = new byte[packet.smbData.Password.Length];
                Array.Copy(packet.smbData.Password, this.smbData.Password, packet.smbParameters.PasswordLength);
            }
            else
            {
                this.smbData.Password = new byte[0];
            }

            if (packet.smbData.Pad != null)
            {
                this.smbData.Pad = new byte[packet.smbData.Pad.Length];
                Array.Copy(packet.smbData.Pad, this.smbData.Pad, packet.smbData.Pad.Length);
            }
            else
            {
                this.smbData.Pad = new byte[0];
            }

            if (packet.smbData.Path != null)
            {
                this.smbData.Path = new byte[packet.smbData.Path.Length];
                Array.Copy(packet.smbData.Path, this.smbData.Path, packet.smbData.Path.Length);
            }
            else
            {
                this.smbData.Path = new byte[0];
            }

            if (packet.smbData.Service != null)
            {
                this.smbData.Service = new byte[packet.smbData.Service.Length];
                Array.Copy(packet.smbData.Service, this.smbData.Service, packet.smbData.Service.Length);
            }
            else
            {
                this.smbData.Service = new byte[0];
            }
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTreeConnectAndxRequestPacket(SmbTreeConnectAndxRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.smbParameters.WordCount      = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand    = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved   = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset     = packet.SmbParameters.AndXOffset;
            this.smbParameters.Flags          = packet.SmbParameters.Flags;
            this.smbParameters.PasswordLength = packet.SmbParameters.PasswordLength;

            this.smbData.ByteCount = packet.SmbData.ByteCount;
            if (packet.smbData.Password != null)
            {
                this.smbData.Password = new byte[packet.smbData.Password.Length];
                Array.Copy(packet.smbData.Password, this.smbData.Password, packet.smbParameters.PasswordLength);
            }
            else
            {
                this.smbData.Password = new byte[0];
            }

            if (packet.smbData.Pad != null)
            {
                this.smbData.Pad = new byte[packet.smbData.Pad.Length];
                Array.Copy(packet.smbData.Pad, this.smbData.Pad, packet.smbData.Pad.Length);
            }
            else
            {
                this.smbData.Pad = new byte[0];
            }

            if (packet.smbData.Path != null)
            {
                this.smbData.Path = new byte[packet.smbData.Path.Length];
                Array.Copy(packet.smbData.Path, this.smbData.Path, packet.smbData.Path.Length);
            }
            else
            {
                this.smbData.Path = new byte[0];
            }

            if (packet.smbData.Service != null)
            {
                this.smbData.Service = new byte[packet.smbData.Service.Length];
                Array.Copy(packet.smbData.Service, this.smbData.Service, packet.smbData.Service.Length);
            }
            else
            {
                this.smbData.Service = new byte[0];
            }
        }
        /// <summary>
        /// to create a TreeConnectAndx request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="smbParametersFlags">USHORT  A 16-bit field used to modify the SMB_COM_TREE_CONNECT_ANDX
        /// request. The client MUST set reserved values to 0, and the server MUST ignore them.</param>
        /// <param name="path">STRING A null-terminated string that represents the server and share name of the resource
        /// to which the client is attempting to connect</param>
        /// <param name="service">The type of resource that the client intends to access</param>
        /// <param name="password">A null-terminated string that represents a share password in plaintext form.</param>
        /// <param name="andxPacket">the andx packet.</param>
        /// <returns>a TreeConnectAndx request packet</returns>
        public SmbTreeConnectAndxRequestPacket CreateTreeConnectAndxRequest(
            ushort messageId,
            ushort uid,
            SmbFlags flags,
            SmbFlags2 flags2,
            TreeConnectAndxFlags smbParametersFlags,
            string path,
            string service,
            byte[] password,
            SmbPacket andxPacket)
        {
            if (path == null)
            {
                path = string.Empty;
            }
            if (service == null)
            {
                service = string.Empty;
            }
            if (password == null)
            {
                password = new byte[0];
            }

            SmbTreeConnectAndxRequestPacket packet = new SmbTreeConnectAndxRequestPacket();

            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TREE_CONNECT_ANDX,
                messageId, uid, 0, flags, flags2);

            SMB_COM_TREE_CONNECT_ANDX_Request_SMB_Parameters smbParameters =
                new SMB_COM_TREE_CONNECT_ANDX_Request_SMB_Parameters();
            if (andxPacket == null)
            {
                smbParameters.AndXCommand = SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            }
            else
            {
                smbParameters.AndXCommand = andxPacket.SmbHeader.Command;
            }
            smbParameters.AndXReserved = 0;
            smbParameters.Flags = (ushort)smbParametersFlags;
            smbParameters.PasswordLength = (ushort)password.Length;
            smbParameters.WordCount = (byte)(Marshal.SizeOf(smbParameters) / NumBytesOfWord);

            SMB_COM_TREE_CONNECT_ANDX_Request_SMB_Data smbData = new SMB_COM_TREE_CONNECT_ANDX_Request_SMB_Data();
            smbData.Password = password;
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
            {
                smbData.Pad = new byte[(smbData.Password.Length + 1) % 2];
                smbData.Path = CifsMessageUtils.ToSmbStringBytes(path, true);
            }
            else
            {
                smbData.Pad = new byte[0];
                smbData.Path = CifsMessageUtils.ToSmbStringBytes(path, false);
            }
            smbData.Service = CifsMessageUtils.ToSmbStringBytes(service, false);
            smbData.ByteCount = (ushort)(smbData.Password.Length + smbData.Pad.Length + smbData.Path.Length
                + smbData.Service.Length);

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.AndxPacket = andxPacket;

            return packet;
        }
        public SmbTreeConnectAndxResponsePacket CreateTreeConnectAndxResponse(
            CifsServerPerConnection connection,
            SmbTreeConnectAndxRequestPacket request,
            OptionalSupport optionalSupport,
            string service,
            SmbPacket andxPacket)
        {
            SmbTreeConnectAndxResponsePacket response = new SmbTreeConnectAndxResponsePacket();
            SmbHeader smbHeader = CifsMessageUtils.CreateSmbHeader(connection, request);
            smbHeader.Tid = connection.GenerateTID();
            response.SmbHeader = smbHeader;

            bool isUnicode = (response.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE;
            byte[] serviceBytes = CifsMessageUtils.ToSmbStringBytes(service, false);
            byte[] fileSystem = CifsMessageUtils.ToSmbStringBytes(CifsMessageUtils.NATIVE_FS, isUnicode);
            int padOffset = Marshal.SizeOf(response.SmbParameters) + sizeof(ushort);
            SMB_COM_TREE_CONNECT_ANDX_Response_SMB_Data smbData = response.SmbData;
            smbData.Pad = new byte[((padOffset + 3) & ~3) - padOffset];
            smbData.Service = serviceBytes;
            smbData.NativeFileSystem = fileSystem;
            smbData.ByteCount = (ushort)(smbData.Pad.Length + smbData.Service.Length
                + smbData.NativeFileSystem.Length);
            response.SmbData = smbData;

            SMB_COM_TREE_CONNECT_ANDX_Response_SMB_Parameters smbParameters = response.SmbParameters;
            smbParameters.AndXCommand =
                andxPacket != null ? andxPacket.SmbHeader.Command : SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            smbParameters.AndXReserved = 0x00;
            smbParameters.AndXOffset = (ushort)(padOffset + response.SmbData.ByteCount);
            smbParameters.OptionalSupport = (ushort)optionalSupport;
            smbParameters.WordCount = (byte)(TypeMarshal.GetBlockMemorySize(smbParameters) / 2);
            response.SmbParameters = smbParameters;

            response.AndxPacket = andxPacket;
            response.UpdateHeader();

            return response;
        }