/// <summary>
        /// Creates a SearchRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="dn">The DN to be searched.</param>
        /// <param name="sizeLimit">Size limit.</param>
        /// <param name="timeLimit">Time limit, in seconds.</param>
        /// <param name="scope">Search scope. Base, single level, or subtree.</param>
        /// <param name="dereferenceAliases">Dereference aliase options.</param>
        /// <param name="filter">Search filter.</param>
        /// <param name="typesOnly">
        /// Specifies whether the search returns only the attribute names without the attribute values.
        /// </param>
        /// <param name="attributes">The attributes to be retrieved.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsSearchRequestPacket CreateSearchRequest(
            AdtsLdapContext context,
            string dn,
            long sizeLimit,
            long timeLimit,
            MsLdap.SearchScope scope,
            MsLdap.DereferenceAlias dereferenceAliases,
            Asn1Choice filter,
            bool typesOnly,
            params string[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            AttributeType[] attributeTypeArray = new AttributeType[length];
            for (int i = 0; i < length; i++)
            {
                attributeTypeArray[i] = new AttributeType(attributes[i]);
            }
            Asn1SequenceOf <AttributeType> attributeList = new Asn1SequenceOf <AttributeType>(attributeTypeArray);

            SearchRequest searchRequest = new SearchRequest(
                new LDAPDN(dn ?? string.Empty),
                new SearchRequest_scope((long)scope),
                new SearchRequest_derefAliases((long)dereferenceAliases),
                new Asn1Integer(sizeLimit),
                new Asn1Integer(timeLimit),
                new Asn1Boolean(typesOnly),
                (Filter)filter,
                attributeList);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchRequest, searchRequest);

            LDAPMessage             message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsSearchRequestPacket packet  = new AdtsSearchRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a SearchRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="dn">The DN to be searched.</param>
        /// <param name="sizeLimit">Size limit.</param>
        /// <param name="timeLimit">Time limit, in seconds.</param>
        /// <param name="scope">Search scope. Base, single level, or subtree.</param>
        /// <param name="dereferenceAliases">Dereference aliase options.</param>
        /// <param name="filter">Search filter.</param>
        /// <param name="typesOnly">
        /// Specifies whether the search returns only the attribute names without the attribute values.
        /// </param>
        /// <param name="attributes">The attributes to be retrieved.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsSearchRequestPacket CreateSearchRequest(
            AdtsLdapContext context,
            string dn,
            long sizeLimit,
            long timeLimit,
            MsLdap.SearchScope scope,
            MsLdap.DereferenceAlias dereferenceAliases,
            Asn1Choice filter,
            bool typesOnly,
            params string[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            AttributeDescription[] attributeDescriptionArray = new AttributeDescription[length];
            for (int i = 0; i < length; i++)
            {
                attributeDescriptionArray[i] = new AttributeDescription(attributes[i]);
            }
            AttributeDescriptionList attributeList = new AttributeDescriptionList(attributeDescriptionArray);

            SearchRequest searchRequest = new SearchRequest(
                new LDAPDN(dn ?? string.Empty),
                new SearchRequest_scope((long)scope),
                new SearchRequest_derefAliases((long)dereferenceAliases),
                new Asn1Integer(sizeLimit),
                new Asn1Integer(timeLimit),
                new Asn1Boolean(typesOnly),
                (Filter)filter,
                attributeList);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();
            operation.SetData(LDAPMessage_protocolOp.searchRequest, searchRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsSearchRequestPacket packet = new AdtsSearchRequestPacket();
            packet.ldapMessagev3 = message;
            packet.messageId = context.MessageId;

            return packet;
        }
        internal AdtsLdapPacket CreatePacketFromType(Type messageType)
        {
            AdtsLdapPacket packet = null;

            if (messageType == typeof(AbandonRequest))
            {
                packet = new AdtsAbandonRequestPacket();
            }
            else if (messageType == typeof(AddRequest))
            {
                packet = new AdtsAddRequestPacket();
            }
            else if (messageType == typeof(AddResponse))
            {
                packet = new AdtsAddResponsePacket();
            }
            else if (messageType == typeof(BindRequest))
            {
                packet = new AdtsBindRequestPacket();
            }
            else if (messageType == typeof(BindResponse))
            {
                packet = new AdtsBindResponsePacket();
            }
            else if (messageType == typeof(CompareRequest))
            {
                packet = new AdtsCompareRequestPacket();
            }
            else if (messageType == typeof(CompareResponse))
            {
                packet = new AdtsCompareResponsePacket();
            }
            else if (messageType == typeof(DelRequest))
            {
                packet = new AdtsDelRequestPacket();
            }
            else if (messageType == typeof(DelResponse))
            {
                packet = new AdtsDelResponsePacket();
            }
            else if (messageType == typeof(ExtendedRequest))
            {
                packet = new AdtsExtendedRequestPacket();
            }
            else if (messageType == typeof(ExtendedResponse))
            {
                packet = new AdtsExtendedResponsePacket();
            }
            else if (messageType == typeof(ModifyDNRequest))
            {
                packet = new AdtsModifyDnRequestPacket();
            }
            else if (messageType == typeof(ModifyDNResponse))
            {
                packet = new AdtsModifyDnResponsePacket();
            }
            else if (messageType == typeof(ModifyRequest))
            {
                packet = new AdtsModifyRequestPacket();
            }
            else if (messageType == typeof(ModifyResponse))
            {
                packet = new AdtsModifyResponsePacket();
            }
            else if (messageType == typeof(SearchRequest))
            {
                packet = new AdtsSearchRequestPacket();
            }
            else if (messageType == typeof(SearchResultEntry))
            {
                packet = new AdtsSearchResultEntryPacket();
            }
            else if (messageType == typeof(SearchResultReference))
            {
                packet = new AdtsSearchResultReferencePacket();
            }
            else if (messageType == typeof(SearchResultDone))
            {
                packet = new AdtsSearchResultDonePacket();
            }
            else if (messageType == typeof(SicilyBindResponse))
            {
                packet = new AdtsSicilyBindResponsePacket();
            }
            else if (messageType == typeof(UnbindRequest))
            {
                packet = new AdtsUnbindRequestPacket();
            }
            else
            {
                throw new StackException("Unknown message type: " + messageType.ToString());
            }

            return packet;
        }
        internal AdtsLdapPacket CreatePacketFromType(Type messageType)
        {
            AdtsLdapPacket packet = null;

            if (messageType == typeof(AbandonRequest))
            {
                packet = new AdtsAbandonRequestPacket();
            }
            else if (messageType == typeof(AddRequest))
            {
                packet = new AdtsAddRequestPacket();
            }
            else if (messageType == typeof(AddResponse))
            {
                packet = new AdtsAddResponsePacket();
            }
            else if (messageType == typeof(BindRequest))
            {
                packet = new AdtsBindRequestPacket();
            }
            else if (messageType == typeof(BindResponse))
            {
                packet = new AdtsBindResponsePacket();
            }
            else if (messageType == typeof(CompareRequest))
            {
                packet = new AdtsCompareRequestPacket();
            }
            else if (messageType == typeof(CompareResponse))
            {
                packet = new AdtsCompareResponsePacket();
            }
            else if (messageType == typeof(DelRequest))
            {
                packet = new AdtsDelRequestPacket();
            }
            else if (messageType == typeof(DelResponse))
            {
                packet = new AdtsDelResponsePacket();
            }
            else if (messageType == typeof(ExtendedRequest))
            {
                packet = new AdtsExtendedRequestPacket();
            }
            else if (messageType == typeof(ExtendedResponse))
            {
                packet = new AdtsExtendedResponsePacket();
            }
            else if (messageType == typeof(ModifyDNRequest))
            {
                packet = new AdtsModifyDnRequestPacket();
            }
            else if (messageType == typeof(ModifyDNResponse))
            {
                packet = new AdtsModifyDnResponsePacket();
            }
            else if (messageType == typeof(ModifyRequest))
            {
                packet = new AdtsModifyRequestPacket();
            }
            else if (messageType == typeof(ModifyResponse))
            {
                packet = new AdtsModifyResponsePacket();
            }
            else if (messageType == typeof(SearchRequest))
            {
                packet = new AdtsSearchRequestPacket();
            }
            else if (messageType == typeof(SearchResultEntry))
            {
                packet = new AdtsSearchResultEntryPacket();
            }
            else if (messageType == typeof(SearchResultReference))
            {
                packet = new AdtsSearchResultReferencePacket();
            }
            else if (messageType == typeof(SearchResultDone))
            {
                packet = new AdtsSearchResultDonePacket();
            }
            else if (messageType == typeof(SicilyBindResponse))
            {
                packet = new AdtsSicilyBindResponsePacket();
            }
            else if (messageType == typeof(UnbindRequest))
            {
                packet = new AdtsUnbindRequestPacket();
            }
            else
            {
                throw new StackException("Unknown message type: " + messageType.ToString());
            }

            return(packet);
        }