//must be called under a lock in receive loop
        public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, ref EndPoint remoteEndPoint, AsyncCallback callback, object state)
        {
            UdpUtility.ValidateBufferBounds(buffer, offset, size);

            ThrowIfNotOpen();

            bool         success     = false;
            IAsyncResult asyncResult = null;

            try
            {
                this.pendingReceiveCount++;

                asyncResult = new ReceiveFromAsyncResult(
                    this.socket,
                    new ArraySegment <byte>(buffer, offset, size),
                    remoteEndPoint,
                    size - offset,
                    this.timeToLive,
                    callback,
                    state);
                success = true;

                return(asyncResult);
            }
            finally
            {
                if (!success)
                {
                    this.pendingReceiveCount--;
                }
            }
        }
            public static ArraySegment <byte> End(IAsyncResult result, ref EndPoint remoteEndPoint)
            {
                ArraySegment <byte> data = TypedAsyncResult <ArraySegment <byte> > .End(result);

                ReceiveFromAsyncResult receiveFromResult = (ReceiveFromAsyncResult)result;

                remoteEndPoint = receiveFromResult.RemoteEndPoint;
                return(data);
            }
            static void OnReceiveMessageFrom(IAsyncResult result)
            {
                if (result.CompletedSynchronously)
                {
                    return;
                }

                ReceiveFromAsyncResult asyncResult = (ReceiveFromAsyncResult)result.AsyncState;

                Exception           completionException = null;
                ArraySegment <byte> data = default(ArraySegment <byte>);

                try
                {
                    data = asyncResult.EndReceiveFrom(result);
                }
                catch (SocketException socketException)
                {
                    completionException = UdpSocket.ConvertNetworkError(socketException, asyncResult);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    completionException = exception;
                }

                if (completionException != null)
                {
                    asyncResult.Complete(false, completionException);
                }
                else
                {
                    asyncResult.Complete(data, false);
                }
            }
 static Exception ConvertNetworkError(SocketException socketException, ReceiveFromAsyncResult result)
 {
     return(ConvertNetworkError(socketException, result.MessageSize, TransferDirection.Receive, result.TimeToLive));
 }
 //must be called under a lock in receive loop
 public ArraySegment <byte> EndReceiveFrom(IAsyncResult result, ref EndPoint remoteEndPoint)
 {
     this.pendingReceiveCount--;
     return(ReceiveFromAsyncResult.End(result, ref remoteEndPoint));
 }