each game uses its own protocol so usually you must send the query request to the server IP:port and if you receive no reply it's probably not online, exactly like I did in all my proof-of-concepts till the end of 2008 (so depending by the game you can check the methods I used in those codes).
exists also another simpler way to check if a server is online but it's not guarantee at 100%, practically:
Code:
if(sendto(sd, "", 0, 0, (struct sockaddr *)&peer, sizeof(struct sockaddr_in)) < 0) {
printf("offline\n");
} else {
printf("online\n");
}
it works because if the server offline your client will receive the icmp port unreachable error but it doesn't work if the server (the system, not the server program) is offline or if you filter the incoming icmp packets.
so although it's useles I wanted to show also this possibility.