How to handle unplugging ethernet cable in socket programming

Hi, I am trying to implement a server-client socket communication. My server and clients are in different PCs. During communications if the ethernet cable is removed from the server side it shows broken pipe due to unable to write on the socket. I want to know how to check whether the ethernet cable is plugged or not before trying to write on socket. Is there any API to check ethernet connectivity that can be used in socket programming?? How can I check a socket is alive or not ??