A lot of software works by sending requests to various ports ( or sockets ), but usually each package has it's own 'language' if you'd like to call it that. The client software ( which is what your talking about ) needs to know what commands to use and how to interpret the responses.
If you look at MySQL - it uses port 3306 by default, so if you know how to communicate with MySQL, you can talk to that port and execute SQL. Note that when you connect to MySQL from PHP - you use