in reply to Catch loss of communication
As far as I know, there's no way to get that information before you attempt the write. In general, under *nix at least, your program doesn't get notified of changes to the status of filehandles until it tries to manipulate the filehandle.