#! /usr/bin/python3
"""Echo squares to a server
"""
import socket
import sys

def echo_square(remote_host):
    """Run a square echo connection
    """
    echo_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        echo_socket.connect((remote_host, 7))
    except socket.error as message:
        print("Unable to connnect to echo server at {}:\n  {}".format(
               remote_host, str(message)))
        return 1
    in_stream = echo_socket.makefile("r")
    out_stream = echo_socket.makefile("w")
    for n in range(0, 16):
        out_stream.write("{0:3}\r\n".format(n*n))
        out_stream.flush()
        print(in_stream.readline().rstrip())
    return 0

if __name__ == "__main__":
    if len(sys.argv) <= 1:
        remote_host = "burnsville.cs.unca.edu"
    else:
        remote_host = sys.argv[1]
    print("Connecting to {}".format(remote_host))
    shell_rc = echo_square(remote_host)
    sys.exit(shell_rc)




