import thread
from socket import * 


HOST, PORT = '', 5353
BUFF = 1024


def handle_client(c, addr):
	while 1:
		c.send("Please enter your name and press enter: \n")
		name = c.recv(BUFF)
		if not name : break
		print "Client name is : ", name
		c.send("Hello %s \n" % name)
		c.send("Welcome to this server %s" % name)
		c.send("\n Type - close - to close connection \n")
		print repr(name) + "recieved :" + repr(name)
		while 1:
			c.send("\nNow each word you typed will be echo back to you.\n")
			data = c.recv(BUFF)
			if not data: break
			print "received data:", data
			c.send("You typed %s \n" % data)	
			if "close" == data.rstrip() : 
				c.send("You asked to leave the server. Goodbye!\n")
				c.close()
		c.close()
		print addr, "- closed connection"


s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind((HOST, PORT))
print "Serving HTTP on port %s ..." % PORT
s.listen(5)

while 1:
	print "Waiting for connection... listening on port", PORT
	c, addr = s.accept()
	print "...connected from:", addr
	thread.start_new_thread(handle_client, (c, addr))
