Any ideas how to implemet a firewall ??

to make it be able to track any application that is trying to listening to a port or any connection request comming in ??