check for socket before close fixes #216