I'm supposing you dont want to do a port scanner but you want to detect if you're being target of a port scanner right?
In that case, the next thing i would do is to check if you receive connection requests from the same ip address but to a large number of different ports...
This is not perfect because the one who is port scanning you can spoof his ip address making connection requests with different ip numbers... but that's up to you to solve (but i dont think there's a real solution to this)
