Socket close/write bug fixes

fixes #1