You can use getsockname to determine IP version,
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Below method can be employed,
/* Function to detect family of a socket */
// Return AF_INET or AF_INET6
unsigned short GetSocketFamily(int sockfd)
{
unsigned short sa[16]; // 32 bytes is enough for sockaddr version for any family
// 16 bytes for IPv4 and 28 bytes for IPv6
socklen_t lth = sizeof(sa);
getsockname(sockfd, (struct sockaddr *)&sa, <h);
return sa[0]; // In any case (IPv4 or IPv6) family is the first halfword of
// address structure
}