Yes, you can determine the PID of the process that connected to your TCP port using the Windows IP Helper API, specifically the GetExtendedTcpTable function.
However, Windows does not provide a direct WinSock API call during the connection accept phase (e.g., in accept() or WSAAccept()) to obtain the PID of the remote process. Instead, you can correlate connection data (local/remote IP and port) with the process list using GetExtendedTcpTable.
Option 1: Use GetExtendedTcpTable (from iphlpapi.dll)
Here’s a high-level summary:
Call accept() on your socket and get the SOCKET.
Use getpeername() to get the remote IP and port.
Use getsockname() to confirm the local port.
Call GetExtendedTcpTable() with TCP_TABLE_OWNER_PID_CONNECTIONS.
Iterate over all entries, find the one matching your local/remote tuple.
The matched entry includes the owning PID of the connecting process.