program listener;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  scktcomp,
  windows,
  frmVNC in 'frmVNC.pas' {Form1},
  untUtils in 'untUtils.pas';

type
  TServer = class
    public
      procedure Startup;
      procedure OnAccept(Sender: TObject; Socket: TCustomWinSocket);
      procedure OnRead(Sender: TObject; Socket: TCustomWinSocket);
      procedure OnDisconnect(Sender: TObject; Socket: TCustomWinSocket);
  end;

var
  strReponse:String;
  scktServer:TServerSocket;
  Server:TServer;
  Msg:TMsg;
procedure TServer.OnAccept(Sender:TObject; Socket:TCustomWinSocket);
begin
  writeln('Connection request:' + Socket.RemoteHost);
end;

procedure TServer.OnRead(Sender: TObject; Socket: TCustomWinSocket);
begin
  writeln('Data: ' + Socket.ReceiveText + ' by ' + socket.RemoteHost);
end;

procedure TServer.OnDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  writeln(Socket.RemoteHost + ' disconnected');
end;

procedure TServer.Startup;
begin
  scktServer := TServerSocket.Create(nil);
  scktServer.Port := StrToInt(strReponse);
  scktServer.OnAccept := OnAccept;
  scktServer.OnClientRead := OnRead;
  scktServer.OnClientDisconnect := OnDisconnect;
  scktServer.Active := True;
end;

begin
  writeln('===============================');
  writeln('Backconnect Plugin by Slayer616');
  writeln('===============================');
  writeln('');
  writeln('Type in Port for listening:');
  readln(strReponse);
  If IsInteger(strReponse) = True then begin
    Server := TServer.Create;
    Server.Startup;
    writeln('Listening...');
    while GetMessage(Msg, 0, 0, 0) do
    begin
      TranslateMessage(Msg);
      DispatchMessage(Msg);
    end;
    Exit;
  end;
  writeln('Invalid Port! Application will be terminated');
  readln(strReponse);
end.
