getsockopt
Prototype:
#include <sys/types.h>
#include <sys/socket.h>
int getsockopt(int sd, int level, int optname, void *optval,
    socklen_t *optlen);
General Description: Get the socket configuration. Socket options available for general, IP, TCP, and TCPv6.
Return Value: Zero (0) if everything goes well. If an error occurs, you can find the cause in errno.
Parameters
sd The socket to read.
level The feature level (SOL_SOCKET, SOL_IP, SOL_TCP, SOL_IPV6).
optname The option to revise.
optval Place for value.
optlen The length of value in bytes. This field is passed by reference.
Possible Errors
EBADF The argument sd is not a valid descriptor.
ENOTSOCK The argument sd is a file, not a socket.
ENOPROTOOPT The option is unknown at the level indicated.
EFAULT The address pointed to optval or optlen is not in a valid part of the process address space.
Examples
int error, size=sizeof(error);
if ( getsockopt(sd, SOL_SOCKET, SO_ERROR, &error, &size) != 0 )
    perror("getsockopt() failed");
printf("socket error=%d\n", error);