make sure socket closes after finishing request or failing