public static unsafe SocketError GetSockOpt(SafeCloseSocket handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue, ref int optionLength)
{
int optLen = optionLength;
Interop.Error err;
if (optionValue == null || optionValue.Length == 0)
{
optLen = 0;
err = Interop.Sys.GetSockOpt(handle, optionLevel, optionName, null, &optLen);
}
else if (optionName == SocketOptionName.Error && optionValue.Length >= sizeof(int))
{
int outError;
SocketError returnError = GetSockOpt(handle, optionLevel, optionName, out outError);
if (returnError == SocketError.Success)
{
fixed (byte* pinnedValue = optionValue)
{
Debug.Assert(BitConverter.IsLittleEndian, "Expected little endian");
*((int*)pinnedValue) = outError;
}
optionLength = sizeof(int);
}
return returnError;
}
else
{
fixed (byte* pinnedValue = optionValue)
{
err = Interop.Sys.GetSockOpt(handle, optionLevel, optionName, pinnedValue, &optLen);
}
}
if (err == Interop.Error.SUCCESS)
{
optionLength = optLen;
return SocketError.Success;
}
return GetSocketErrorForErrorCode(err);
}