Do not close the socket in terminate if it is undefined. (it was already closed). Fix one test case.