Core/Network: Fix some possible exceptions on socket.close()

Ref #12634