Q:

How the ports really work?

I'm bit confused with the concepts of ports. Lets say that I opened inbound port for 3306 (mysql) to my-ip. So my-ip will make a connection to 3306. But I wonder in-turn how mysql speaks to my ip?
Say for example lets my application from my-ip is querying mysql, how mysql sends the data back to my my-ip machine? I mean it again uses port?
Thanks in advance.

A:

A TCP session is defined by 4 bits of information: a pair of IP addresses, and a pair of ports.
When you open a connection in firefox to stackoverflow.com, your operating system allocates an unused port to that connection. Usually something fairly high, say 32012. stackoverflow.com is listening on port 80, so no choice there.
The TCP session is therefore you:32012 - stackoverflow.com:80. This connection is used bi-directionally. So your HTTP requests are sent to stackoverflow.com:80, and the HTTP responses are sent to you:32012.

