This is the domain of a packet disassembler & protocol analyzer. To glean insight, use a real one such as
Wireshark, and it may reveal something about how it operates. I doubt that one can be made to be fool-proof, since the actual application that receives a packet is the only thing that knows for sure how to parse the packet. Commonly used and documented protocols will have some kind of identifiable signature against which the protocol analyzer must compare each packet, or series of packets. The port number is most likely used as a strong hint in the analysis. It will be a very big job to create a competent analyzer, even for a small number of well known protocols. If you simply want the information that such a tool provides, it will be a lot easier to use a ready-made one. If you are interested in a particular protocol for which no template exists, it would probably be welcomed if you were to develop and contribute even a single one.
--- rod.