public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
{
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
byte[] optionValue = new byte[optionLength];
int realOptionLength = optionLength;
// This can throw ObjectDisposedException.
SocketError errorCode = SocketPal.GetSockOpt(
_handle,
optionLevel,
optionName,
optionValue,
ref realOptionLength);
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.getsockopt returns errorCode:{errorCode}");
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
if (optionLength != realOptionLength)
{
byte[] newOptionValue = new byte[realOptionLength];
Buffer.BlockCopy(optionValue, 0, newOptionValue, 0, realOptionLength);
optionValue = newOptionValue;
}
return optionValue;
}