Added socket closing to the error flow