Where you see the \"; .sockclose $sockname\", you could replace that with inc %var, and then if (%var == 3) sockclose $sockname