Closing socket before exception