Refactoring of socket disconnect code.