properly close the context and socket