Try the sources of "netstat" instead of lsof. It's probably simpler and also GPL.
Example:
netstat -plan
gives all connected and listening sockets (tcp, udp, and filesy sockets) including PID, and process name. (when not run as root, it will give you this info only for the processes of the user running netstat, but there's no way around that anyway)
"netstat" is (on debian) part of the "net-tools" package. One place to get the sources:
http://ftp.debian.org/debian/pool/ma...60.orig.tar.gz