refactor socket close function

See #144

I am not sure if this is a relevant bug fix, or a portability fix, or else.