in reply to explore tcp/ip stack

I guess, by stack, you mean protocol-stack? What platform, BTW (I assume *nix for a while)?

There's a loose coupling between TCP port-number and the underlying protocol, like documented here. But to be sure, you need to inspect some incoming/outgoing PDUs. pcap might come handy or wireshark.

Digging deeper, your Perl program could analyse the output of tshark (-n -V -S -l) for example. Here's a sample of

tshark -n -V -S -l | perl -ne 'print if /^\S/'
... Frame 82 (66 bytes on wire, 66 bytes captured) Ethernet II, Src: 01:16:XX:XX:XX:a4 (01:16:XX:XX:XX:a9), Dst: 00:XX:XX +:XX:d5:e6 (00:XX:XX:XX:d5:e6) Internet Protocol, Src: 192.168.XX.XXX (192.168.XX.XXX), Dst: 166.XX.X +X.XX (166.XX.XX.XX) Transmission Control Protocol, Src Port: 46289 (46289), Dst Port: 80 ( +80), Seq: 592, Ack: 176, Len: 0 ...
Looks like something, that can be converted into a stack...(some parts anonymised by using XX).

HTH