Using connection-oriented sockets
In the .NET Framework, you can create connection-oriented communications with remote hosts across a network. To create a connection-oriented socket, separate sequences of functions must be used for server programs and client programs:

Server
You have four tasks to perform before a server can transfer data with a client connection:
- Create a socket.
- Bind the socket to a local
IPEndPoint.
- Place the socket in listen mode.
- Accept an incoming connection on the socket.
Creating the server
The first step to constructing a TCP server is to create an instance of the Socket object. The other three functions necessary for successful server operations are then accomplished by using the methods of Socket object. The following C# code snippet includes these steps:
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 8000);
Socket newsock = Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(localEndPoint);
newsock.Listen(10);
Socket client = newsock.Accept();
The Socket object created by the Accept() method can now be used to transmit data in either direction between the server and the remote client.
Client
Now that you have a working TCP server, you can create a simple TCP client program to interact with it. There are only two steps required to connect a client program to a TCP server:
- Create a socket.
- Connect the socket to the remote server address.
Creating the client
As it was for the server program, the first step for creating the client program is to create a Socket object. The Socket object is used by the socket Connect() method to connect the socket to a remote host:
IPEndPoint ipep =
new IPEndPoint(Ipaddress.Parse("127.0.0.1"), 8000);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Connect(ipep);refer http://www.codeproject.com/KB/IP/TCPIPChat.aspx for details.