Refactor client sending when socket closes
Move exception handling out of tight loop