import sublime, sublime_plugin
from . import Session
import socket
import sys

class DiffListener(sublime_plugin.EventListener):
    """Listens for modifications to the view and gets the diffs using 
    Operational Transformation"""

    def __init__(self):
        # watched_views is a sessions of which currently open views are bound to 
        # remote-collab sessions. This allows the  EventListener to check if
        # on_modified events happened to the views it cares about, or to other 
        # views which it doesn't care about.
        self.sessions = []

    def on_modified_async(self, view):
        """Listens for modifications to views which are part of a currently
        active remote session."""
        if self.sessions:
            for session in self.sessions:
                if session.view is view:
                    current_buffer = view.substr(sublime.Region(0, view.size())) 
                    session.send_diffs(current_buffer)  

    def on_close(self, view): 
        """Check to see if views I care about are closed, and if they are,
        drop them from my watched-views"""
        if view in sessions.keys():
            del sessions[view]      

class StartSessionCommand(sublime_plugin.TextCommand):
    """Command to start a new RemoteCollab session for the current view"""
    get_buffer = lambda view: view.substr(sublime.Region(0, view.size()))

    def __init__(self, *args, **kwargs):
        self.df = DiffListener()
        sublime_plugin.TextCommand.__init__(self, *args, **kwargs)

    def run(self, edit):     
        # this will have to connect to the remote server (getting the address
        # from the settings file), wait for the server to generate the session,
        # and tell the user the access token. it'll then have to start watching
        # the urrent view synchronizing
        session = Session(self.view, True)
        df.sessions.append(session)
        #session.patch_listener()
            
class ConnectToSessionCommand(sublime_plugin.ApplicationCommand):
    """Command to connect to an external RemoteCollab session."""
    def __init__(self, *args, **kwargs):
        self.df = DiffListener()
        sublime_plugin.ApplicationCommand.__init__(self, *args, **kwargs)

    # this will have to connect to the remote server (configured in settings file),
    # send the session token, make a new view containing the contents of the remote
    # session, and then start listening for modifications to that view and synchronizing   
    def run(self):
        session = Session(self.view, False)
        df.sessions.append(session)
        #session.patch_listener()

class CloseSessionCommand(sublime_plugin.TextCommand):
    """Command to close a RemoteCollab session."""
    def __init__(self, *args, **kwargs):
        sublime_plugin.ApplicationCommand.__init__(self, *args, **kwargs)

    # this will have to connect to the remote server (configured in settings file),
    # send the session token, make a new view containing the contents of the remote
    # session, and then start listening for modifications to that view and synchronizing   
    def run(self):
        session = next((session for session in DiffListener.sessions if session.view is self.view), None)
        if session is not None:
            session.end_session()

@@ -2007,35 +2007,30 @@
 lime_plugin.
-Application
+Window
 Command):%0A  
@@ -2513,32 +2513,327 @@
  def run(self):%0A
+        self.window.show_input_panel(%0A            'Session IP Address',%0A            '',%0A            self.on_done,%0A            self.on_change,%0A            self.on_cancel)%0A%0A    def on_done(self, input):%0A        %22%22%22Input panel handler - creates a new session connected to the given IP address. %22%22%22%0A
         session 
@@ -2847,20 +2847,33 @@
 on(self.
-view
+window.new_file()
 , False)

Make Connect command prompt for IP