title: Develope a Gateway with Telethon Library - (Telegram)
body: <p>I need to develope a Gateway using the Telethon library:
  <a href="https://github.com/LonamiWebs/Telethon" rel="nofollow noreferrer">https://github.com/LonamiWebs/Telethon</a></p>
  
  <p>The Gateway has to create a server socket where a telegram client is connected.
  When the Gateway receives a message from the socket (by the client), it sends the specific message to the Telegram server(through Telethon). When it receives an update from the telegram server, it sends the message to the client through the socket.</p>
  
  <p>I have started with this code:</p>
  
  <pre><code>async def main(socket):    
                client = await TelegramClient(session_name, api_id, api_hash).start()
  
             @client.on(events.NewMessage)
             async def handler(event):
                   #new message, sending to the socket...
                   try:
                     mess = event.message.message
                     message = (mess).encode()
                     socket.send(message)
                   except Exception as e: 
                     print("Exception: ",e)
  
              await client.run_until_disconnected()
  </code></pre>
  
  <p><b>MAIN--></b></p>
  
  <pre><code>print("Start program...")    
  HOST = '127.0.0.1'
  PORT = 65436
  print("Waiting connection at the port: ",PORT)
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen(5)
  conn, addr = s.accept()
  print("Connection OK!")    
  asyncio.get_event_loop().run_until_complete(main(conn))
  </code></pre>
  
  <p>This code, when receives a new message from the telegram server, will send correctly the message (through the socket) to the telegram client.
  The problem is that I don't known how to also listen from the socket and, when I receive a message (by the socket client), send it to the telegram server.</p>
  
  <p>P.S. After the row <i>await client.run_until_disconnected()</i> I can't do anything. </p>
  
  <p>Thank you very much!!!</p>