When two applications sitting on two networks connect with each other, they need to know each other’s addresses first.
Assume one private as your home network and the other one public as the internet. We know that private networks and public networks are separated by a gateway that manages the connections.
Gateway has two interfaces. One connects with the private network and the other with the public network. Each interface has a corresponding address. The private interface has a private address and the public interface has a public address.
The address of the private interface is also known as the gateway address for all machines on the private network, including your computer.
When two applications sitting on distant machines on different networks try to connect with each other, they need something more than the IP addresses.
IP addresses are assigned to machines, not to the applications. There may be multiple applications on a machine trying to reach out.
To create the complete address of an application, a PORT address is added to the IP address.
There are 65535 addresses available for port out of which 1024 are reserved and predefined for some applications such as web servers.
Thus, the web browser on your machine has an IP and a Port. This is a private address, however. Similarly, the webserver on the other side of the internet also has an IP and a port. This is a public address, mapped to a URL.
The gateway converts the private address of your web browser (Private IP address and Port) into a public IP address and port. This is required because a private address is not visible to the public network and the web server cannot communicate with a private address.
Hence, the gateway converts and maps the private address into a public address and enables communication with the webserver sitting on the internet.
This is known as Network Address and Port Translation or NAPT.