I think your code have a small error in the pseudotty definition for input (it should be '<pty<', not '>pty>'), so I wuold say that the terminal output you expected in $out is going to $in.
It's just a guess. The second guess would be that you program is looking in its first atempt to read from a output pseudo tty.
Try correcting this, and see if you run still don't send the tty output to $out.