And what if the connexion is not closed gracefully ? (which is the case i'm facing)
 
Please google "tcp detect cable disconnection" to understand the difficulty of this issue.