Working version. Added socket and context teardown in termination