public static object QueryContextAttributes(SecurityContext securityContext, ContextAttribute contextAttribute)
        {
            int cb;

            if (contextAttribute != ContextAttribute.Sizes)
            {
                if (contextAttribute != ContextAttribute.StreamSizes)
                {
                    throw new NotImplementedException();
                }
                cb = 20;
            }
            else
            {
                cb = 16;
            }
            IntPtr intPtr = Marshal.AllocHGlobal(cb);
            object result;

            try
            {
                int num = UnsafeNclNativeMethods.NativeNTSSPI.QueryContextAttributes(ref securityContext.Handle, (int)contextAttribute, intPtr);
                if (num != 0)
                {
                    throw new Win32Exception(num);
                }
                object obj = null;
                if (contextAttribute != ContextAttribute.Sizes)
                {
                    if (contextAttribute == ContextAttribute.StreamSizes)
                    {
                        obj = new StreamSizes(intPtr);
                    }
                }
                else
                {
                    obj = new Sizes(intPtr);
                }
                result = obj;
            }
            finally
            {
                Marshal.FreeHGlobal(intPtr);
            }
            return(result);
        }
        internal TcpSecureStream(TcpStream tcpStream, SecurityContext securityContext) : base(tcpStream)
        {
            if (securityContext == null)
            {
                throw new ArgumentNullException("securityContext");
            }
            this.securityContext = securityContext;
            try
            {
                switch (this.securityContext.SecurityContextMode)
                {
                case SecurityContextMode.block:
                {
                    Sizes sizes = (Sizes)SSPIWrapper.QueryContextAttributes(securityContext, ContextAttribute.Sizes);
                    this.maxTokenSize            = Math.Max(sizes.cbSecurityTrailer, sizes.cbMaxSignature);
                    this.maxEncryptionBufferSize = Math.Min(sizes.cbMaxToken, 65535);
                    if (this.maxTokenSize > 65535)
                    {
                        throw new XmlaStreamException(XmlaSR.TcpStream_MaxSignatureExceedsProtocolLimit);
                    }
                    this.tokenBufferForWrite = new byte[this.maxTokenSize];
                    this.tokenBufferForRead  = new byte[this.maxTokenSize];
                    this.securityBuffers     = new SecurityBufferClass[]
                    {
                        new SecurityBufferClass(null, BufferType.Data),
                        new SecurityBufferClass(this.tokenBufferForWrite, BufferType.Token)
                    };
                    break;
                }

                case SecurityContextMode.stream:
                {
                    this.streamSizes = (StreamSizes)SSPIWrapper.QueryContextAttributes(securityContext, ContextAttribute.StreamSizes);
                    if (this.streamSizes.cbMaxMessage > 65535)
                    {
                        throw new XmlaStreamException(XmlaSR.TcpStream_MaxSignatureExceedsProtocolLimit);
                    }
                    this.streamHeaderForWrite  = new ArraySegment <byte>(new byte[this.streamSizes.cbHeader]);
                    this.streamTrailerForWrite = new ArraySegment <byte>(new byte[this.streamSizes.cbTrailer]);
                    this.streamEncryptedDataAccumulatorForRead            = new List <ArraySegment <byte> >();
                    this.streamEncryptedDataAccumulatorForReadFreeBuffers = new List <ArraySegment <byte> >();
                    this.streamDecryptedDataForRead = new List <ArraySegment <byte> >();
                    SecurityBufferClass securityBufferClass  = new SecurityBufferClass(this.streamHeaderForWrite.Array, BufferType.Header);
                    SecurityBufferClass securityBufferClass2 = new SecurityBufferClass(null, BufferType.Data);
                    SecurityBufferClass securityBufferClass3 = new SecurityBufferClass(this.streamTrailerForWrite.Array, BufferType.Trailer);
                    SecurityBufferClass securityBufferClass4 = new SecurityBufferClass(null, BufferType.Empty);
                    this.securityBuffers = new SecurityBufferClass[]
                    {
                        securityBufferClass,
                        securityBufferClass2,
                        securityBufferClass3,
                        securityBufferClass4
                    };
                    break;
                }

                default:
                    throw new XmlaStreamException("SecurityContextMode " + this.securityContext.SecurityContextMode + " not configured!");
                }
            }
            catch (XmlaStreamException)
            {
                throw;
            }
            catch (IOException innerException)
            {
                throw new XmlaStreamException(innerException);
            }
            catch (SocketException innerException2)
            {
                throw new XmlaStreamException(innerException2);
            }
            catch (Win32Exception innerException3)
            {
                throw new XmlaStreamException(innerException3);
            }
        }