To answer your question it is done through calls to the operating system (usually through BSD style sockets know as Berkeley Sockets Interface) by creating a socket() and then using bind() to bind a name to the socket which can then be used as an endpoint for communication which the shell uses for input and output.

I reccomend reading the following *nix man entries or the finding the winsock equivalent(for Windows socket programming):

connect(2), listen(2), socket(2), getsockname(2), accept(2), bind(2), connect(2), getprotoent(3), getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2), select(2), send(2),
shutdown(2), socketpair(2), write(2)


-Maestr0


Edit: I see your question has already been answered, I was a bit late.