public static unsafe SocketError GetSockOpt(SafeCloseSocket handle, SocketOptionLevel optionLevel, SocketOptionName optionName, out int optionValue)
{
if (optionLevel == SocketOptionLevel.Socket)
{
if (optionName == SocketOptionName.ReceiveTimeout)
{
optionValue = handle.ReceiveTimeout == -1 ? 0 : handle.ReceiveTimeout;
return SocketError.Success;
}
else if (optionName == SocketOptionName.SendTimeout)
{
optionValue = handle.SendTimeout == -1 ? 0 : handle.SendTimeout;
return SocketError.Success;
}
}
if (optionName == SocketOptionName.Error)
{
Interop.Error socketError = default(Interop.Error);
Interop.Error getErrorError = Interop.Sys.GetSocketErrorOption(handle, &socketError);
optionValue = (int)GetSocketErrorForErrorCode(socketError);
return getErrorError == Interop.Error.SUCCESS ? SocketError.Success : GetSocketErrorForErrorCode(getErrorError);
}
int value = 0;
int optLen = sizeof(int);
Interop.Error err = Interop.Sys.GetSockOpt(handle, optionLevel, optionName, (byte*)&value, &optLen);
optionValue = value;
return err == Interop.Error.SUCCESS ? SocketError.Success : GetSocketErrorForErrorCode(err);
}