Look for examples of telnet, client and server code.
Further, research the SMTP and POP3. These are the industry standard protocols that perform these functions. These protocols run on top of a basic telnet session. you can actually telnet to a server and create emails by typing in the data.
And finally, buy the book TCP/IP Illustrated, Vol. 1
. If you're going to do TCP programming, you must have this book.