in reply to Programming and Instant Gratification

Secondary to the point: if you're on Unix, the program lsof will do this for you. Despite its name, it also lists open ports and what processes are holding them open (lsof -i). It's not perl, but it should definitely be in your toolbox. I've found it indispensible.

Chris
M-x auto-bs-mode