Normally I’d use IP Helper API for this purpose. For the TCP protocol it can be done with the following steps:
1) Use GetExtendedTcpTable and GetOwnerModuleFromTcpEntry to build the mapping from the local (IP address, TCP port) to process executable.
2) Extract IP and port information from the packet and use the mapping built on previous step to look up the process executable.
3) Update the mapping periodically or when you can’t lookup process for the certain packet.
For the UDP just use GetExtendedUdpTable and GetOwnerModuleFromUdpEntry instead.