## Server side socket check
import sys, string
from socket import *
serverHost = 'geppetto.ph.utexas.edu'
serverPort = 10000


#message = [flags,modifiers,xpos,ypos,xchan,ychan,posTime,
#           click,clickPos1,clickPos2,release,releasePos1,releasePos2,
#           releasePosLast1,releasePosLast2,clickTime,clickTimeLast,
#           releaseTime,releaseTimeLast,winsizeX,winsizeY,inwin,procid]

def main(flags,modifiers,xpos,ypos,xchan,ychan,posTime,click,clickpos1,clickpos2,clickposlast1,clickposlast2,release,rel_pos1,rel_pos2,rel_poslast1,rel_poslast2,clickTime,clickTimeLast,relTime,relTimeLast,winsizeX,winsizeY,inWin):

  table = {}
  procID = '0'

  message = ['flags::' + str(flags) + '__' + procID,
             'modifiers::' + str(modifiers) + '__' + procID,
             'xpos::' + str(xpos) + '__' + procID,
             'ypos::' + str(ypos) + '__' + procID,
             'xchan::' + str(xchan) + '__' + procID,
             'ychan::' + str(ychan) + '__' + procID,
             'posTime::' + str(posTime) + '__' + procID,
             'click::' + str(click) + '__' + procID,
             'clickpos1::' + str(clickpos1) + '__' + procID,
             'clickpos2::' + str(clickpos2) + '__' + procID,
             'clickposlast1::' + str(clickposlast1) + '__' + procID,
             'clickposlast2::' + str(clickposlast2) + '__' + procID,
             'release::' + str(release) + '__' + procID,
             'rel_pos1::' + str(rel_pos1) + '__' + procID,
             'rel_pos2::' + str(rel_pos2) + '__' + procID,
             'rel_poslast1::' + str(rel_poslast1) + '__' + procID,
             'rel_poslast2::' + str(rel_poslast2) + '__' + procID,
             'clickTime::' + str(clickTime) + '__' + procID,
             'clickTimeLast::' + str(clickTimeLast) + '__' + procID,
             'relTime::' + str(relTime) + '__' + procID,
             'relTimeLast::' + str(relTimeLast) + '__' + procID,
             'winsizeX::' + str(winsizeX) + '__' + procID,
             'winsizeY::' + str(winsizeY) + '__' + procID,
             'inWin::' + str(inWin) + '__' + procID]

  sockobj = socket(AF_INET, SOCK_STREAM)
  sockobj.connect((serverHost, serverPort))

  for line in message:
    sockobj.send(line)
    data = sockobj.recv(1024)
#    print 'Client received:', data

  sockobj.close()

  myHost = ''
  myPort = 20001
  recv_message = []

  sockobj = socket(AF_INET, SOCK_STREAM)
  sockobj.bind((myHost, myPort))
  sockobj.listen(5)

  while 1:
    connection, address = sockobj.accept()
#    print 'Server connected by', address
    while 1:
      data = connection.recv(1024)
      if not data: break
      else:
        recv_message.append(data) 
      connection.send('Echo')
    connection.close()
    if (len(recv_message) == 24): break

  for indy in recv_message:
    IDkey,value = string.split(indy,'::')  
    table[IDkey] = value

  return string.atoi(table['flags']),string.atoi(table['modifiers']),string.atoi(table['xpos']),string.atoi(table['ypos']),string.atof(table['xchan']),string.atof(table['ychan']),string.atof(table['posTime']),string.atoi(table['click']),string.atoi(table['clickpos1']),string.atoi(table['clickpos2']),string.atoi(table['clickposlast1']),string.atoi(table['clickposlast2']),string.atoi(table['release']),string.atoi(table['rel_pos1']),string.atoi(table['rel_pos2']),string.atoi(table['rel_poslast1']),string.atoi(table['rel_poslast2']),string.atof(table['clickTime']),string.atof(table['clickTimeLast']),string.atof(table['relTime']),string.atof(table['relTimeLast']),string.atoi(table['winsizeX']),string.atoi(table['winsizeY']),string.atoi(table['inWin'])
