So the return type of this function is 'closed' | #state{}. This feels a little odd to me - How about {closed, State#state{sock=undefined}} | {not_closed, State#state{idle_tref=NewRef}} ? This way the return type is {Info, #state{}} in all cases.