Closes socket correctly