// File: vianna274[P]dropbox/src/Server.cpp
// Function: Dropbox::Server::connectNewClient()
void Server::connectNewClient()
{

    MessageData *d = connectClientSocket.receive(TIMEOUT_OFF);
    if (d->type == TYPE_MAKE_CONNECTION) {
        string username(d->username);
        string userIp(d->payload);
        User *user = getUser(username);
        if(user == nullptr){
            user = new User(username, rootDir+username+"/");
            users.push_back(user);
        }
        else{
            user->lockDevices();
            if(user->getNumDevicesConnected() == MAX_DEVICES){
                refuseOverLimitClient(user);
                user->unlockDevices();
                return;
            }
            user->unlockDevices();
        }
        int newPort = getAvailablePort();
        MessageData packet = make_packet(TYPE_MAKE_CONNECTION, 1, 1, -1, to_string(newPort).c_str());
	    WrapperSocket *socket = new WrapperSocket(newPort);
        connectClientSocket.send(&packet);
        
        user->lockDevices();
        user->addDevice(socket);
        user->unlockDevices();

        this->propagateConnection(username, userIp);
        thread listenToClientThread(&Server::listenToClient, this, socket, user);
        listenToClientThread.detach();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x11b0, %rsp           # imm = 0x11B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x1168(%rbp)
addq $0xe8, %rdi
xorl %esi, %esi
callq 0x18090
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xe174
movq -0x10(%rbp), %rax
addq $0xfb4, %rax            # imm = 0xFB4
movq %rax, -0x1178(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x1170(%rbp)
callq 0x3620
movq -0x1178(%rbp), %rsi
movq -0x1170(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x3480
jmp 0xdc63
leaq -0x31(%rbp), %rdi
callq 0x33e0
movq -0x10(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x1188(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x1180(%rbp)
callq 0x3620
movq -0x1188(%rbp), %rsi
movq -0x1180(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x3480
jmp 0xdca4
leaq -0x69(%rbp), %rdi
callq 0x33e0
leaq -0x98(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3190
jmp 0xdcbf
movq -0x1168(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xf3b0
movq %rax, -0x1190(%rbp)
jmp 0xdcdb
leaq -0x98(%rbp), %rdi
callq 0x3280
movq -0x1190(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0xdea6
movl $0x98, %edi
callq 0x33b0
movq %rax, -0x1198(%rbp)
jmp 0xdd10
movb $0x1, -0xf9(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3190
jmp 0xdd29
movq -0x1168(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x7210
jmp 0xdd42
leaq 0xb6be(%rip), %rdx      # 0x19407
leaq -0xd8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x6730
jmp 0xdd5e
movq -0x1198(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x16aa0
jmp 0xdd7a
movq -0x1198(%rbp), %rax
movb $0x0, -0xf9(%rbp)
movq %rax, -0x78(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3280
leaq -0xf8(%rbp), %rdi
callq 0x3280
leaq -0xb8(%rbp), %rdi
callq 0x3280
movq -0x1168(%rbp), %rdi
addq $0x958, %rdi            # imm = 0x958
leaq -0x78(%rbp), %rsi
callq 0x12360
jmp 0xddc9
jmp 0xdf06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x33e0
jmp 0xe17d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x33e0
jmp 0xe169
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xe160
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x3280
jmp 0xe160
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xde8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xde7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xde72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3280
leaq -0xf8(%rbp), %rdi
callq 0x3280
leaq -0xb8(%rbp), %rdi
callq 0x3280
testb $0x1, -0xf9(%rbp)
jne 0xde95
jmp 0xdea1
movq -0x1198(%rbp), %rdi
callq 0x3370
jmp 0xe160
movq -0x78(%rbp), %rdi
callq 0x12440
jmp 0xdeb1
movq -0x78(%rbp), %rdi
callq 0x17070
movl %eax, -0x119c(%rbp)
jmp 0xdec2
movl -0x119c(%rbp), %eax
cmpl $0x2, %eax
jne 0xdef9
movq -0x1168(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0xf480
jmp 0xdedf
movq -0x78(%rbp), %rdi
callq 0x12460
jmp 0xdeea
movl $0x1, -0x100(%rbp)
jmp 0xe0b8
movq -0x78(%rbp), %rdi
callq 0x12460
jmp 0xdf04
jmp 0xdf06
movq -0x1168(%rbp), %rdi
callq 0xf030
movl %eax, -0x11a0(%rbp)
jmp 0xdf1a
movl -0x11a0(%rbp), %eax
movl %eax, -0x104(%rbp)
movl -0x104(%rbp), %esi
leaq -0x10f8(%rbp), %rdi
callq 0x6e00
jmp 0xdf3a
leaq -0x10f8(%rbp), %rdi
callq 0x3180
movq %rax, %r9
leaq -0x10d8(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %ecx, %edx
callq 0x97a0
jmp 0xdf69
leaq -0x10f8(%rbp), %rdi
callq 0x3280
movl $0x48, %edi
callq 0x33b0
movq %rax, -0x11a8(%rbp)
jmp 0xdf88
movq -0x11a8(%rbp), %rdi
movl -0x104(%rbp), %esi
callq 0x177a0
jmp 0xdf9c
movq -0x1168(%rbp), %rdi
movq -0x11a8(%rbp), %rax
movq %rax, -0x1100(%rbp)
addq $0xe8, %rdi
leaq -0x10d8(%rbp), %rsi
callq 0x17d90
jmp 0xdfc6
movq -0x78(%rbp), %rdi
callq 0x12440
jmp 0xdfd1
movq -0x78(%rbp), %rdi
movq -0x1100(%rbp), %rsi
callq 0x17090
jmp 0xdfe3
movq -0x78(%rbp), %rdi
callq 0x12460
jmp 0xdfee
leaq -0x1120(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3190
jmp 0xe000
leaq -0x1140(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3190
jmp 0xe012
movq -0x1168(%rbp), %rdi
leaq -0x1120(%rbp), %rsi
leaq -0x1140(%rbp), %rdx
callq 0xf640
jmp 0xe02e
leaq -0x1140(%rbp), %rdi
callq 0x3280
leaq -0x1120(%rbp), %rdi
callq 0x3280
movq -0x1168(%rbp), %rax
leaq 0x171c(%rip), %rcx      # 0xf770
movq %rcx, -0x1158(%rbp)
movq $0x0, -0x1150(%rbp)
movq %rax, -0x1160(%rbp)
leaq -0x1148(%rbp), %rdi
leaq -0x1158(%rbp), %rsi
leaq -0x1160(%rbp), %rdx
leaq -0x1100(%rbp), %rcx
leaq -0x78(%rbp), %r8
callq 0x12480
jmp 0xe094
leaq -0x1148(%rbp), %rdi
callq 0x3600
jmp 0xe0a2
leaq -0x1148(%rbp), %rdi
callq 0x69d0
movl $0x0, -0x100(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3280
leaq -0x30(%rbp), %rdi
callq 0x3280
movl -0x100(%rbp), %eax
testl %eax, %eax
je 0xe0db
jmp 0xe0d6
jmp 0xe174
jmp 0xe174
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x10f8(%rbp), %rdi
callq 0x3280
jmp 0xe160
movq -0x11a8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3370
jmp 0xe160
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xe13a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1140(%rbp), %rdi
callq 0x3280
leaq -0x1120(%rbp), %rdi
callq 0x3280
jmp 0xe160
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1148(%rbp), %rdi
callq 0x69d0
leaq -0x68(%rbp), %rdi
callq 0x3280
leaq -0x30(%rbp), %rdi
callq 0x3280
jmp 0xe17d
addq $0x11b0, %rsp           # imm = 0x11B0
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3610
nopw %cs:(%rax,%rax)