System.Net.ConnectStream.BeginRead C# (CSharp) Method

BeginRead() private method

private BeginRead ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
size int
callback AsyncCallback
state object
return IAsyncResult
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state) {
#if DEBUG
            using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) {
#endif
            GlobalLog.Print("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginRead() " + ValidationHelper.HashString(m_Connection) + ", " + offset.ToString() + ", " + size.ToString());
            if(Logging.On)Logging.Enter(Logging.Web, this, "BeginRead", "");

            //
            // parameter validation
            //
            if (WriteStream) {
                throw new NotSupportedException(SR.GetString(SR.net_writeonlystream));
            }
            if (buffer==null) {
                throw new ArgumentNullException("buffer");
            }
            if (offset<0 || offset>buffer.Length) {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size<0 || size>buffer.Length-offset) {
                throw new ArgumentOutOfRangeException("size");
            }

            //
            // if we have a stream error, or we've already shut down this socket
            //  then we must prevent new BeginRead/BeginWrite's from getting
            //  submited to the socket, since we've already closed the stream.
            //

            if (ErrorInStream) {
                throw m_ErrorException;
            }

            if (IsClosed) {
                throw new WebException(
                            NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.ConnectionClosed),
                            WebExceptionStatus.ConnectionClosed);
            }

            if (m_Request.Aborted) {
                GlobalLog.Print("ConnectStream#" + ValidationHelper.HashString(this) + "::InternalWrite() throwing");
                throw new WebException(
                            NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled),
                            WebExceptionStatus.RequestCanceled);
            }         


            //
            // if we fail/hang this call for some reason,
            // this Nesting count we be non-0, so that when we
            // close this stream, we will abort the socket.
            //

            int nesting = Interlocked.CompareExchange(ref m_CallNesting, Nesting.IoInProgress, Nesting.Idle);
            GlobalLog.Print("BeginRead() In: callNesting : " + m_CallNesting.ToString());

            if (nesting != 0)
            {
                throw new NotSupportedException(SR.GetString(SR.net_no_concurrent_io_allowed));
            }

            IAsyncResult result =
                BeginReadWithoutValidation(
                        buffer,
                        offset,
                        size,
                        callback,
                        state);

            if(Logging.On)Logging.Exit(Logging.Web, this, "BeginRead", result);
            return result;
#if DEBUG
            }
#endif
        }