public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, object state)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (remoteEP == null)
{
throw new ArgumentNullException(nameof(remoteEP));
}
if (offset < 0 || offset > buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (size < 0 || size > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
// This will check the permissions for connect.
EndPoint endPointSnapshot = remoteEP;
Internals.SocketAddress socketAddress = CheckCacheRemote(ref endPointSnapshot, false);
// Set up the async result and indicate to flow the context.
OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback);
asyncResult.StartPostingAsyncOp(false);
// Post the send.
DoBeginSendTo(buffer, offset, size, socketFlags, endPointSnapshot, socketAddress, asyncResult);
// Finish, possibly posting the callback. The callback won't be posted before this point is reached.
asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache);
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, asyncResult);
return asyncResult;
}