It's quite logical that you have to place the $sock->print() outside of the while loop for two reasons:
- First you do the request, THEN you get an answer
- <$sock> will not have anything before you do the print on $socj so the while (<$sock>) will exit immediately if you did not do the print before it.