in reply to Read output from tcpdump

Just a suggestion (not a solution), why not use:
sytem 'netstat -ntu';
I aware that this drags you away from your original question, but I feel netstat is much more cool, particularly, for the purpose you mentioned in your original post.