// File: amit-mittal[P]FTP/ftpClient.cpp
// Function: ftpClient::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
int ftpClient::connect(string serverIP, int controlPort){
	c_socket.setIp(serverIP);
	c_socket.setPort(controlPort);
	c_socket.setHints(0);
	if(c_socket.connect() != 0)
		return 1;
	cout << "Successfully connected to server (control socket): " << serverIP << endl;

	tcpSocket d_socket_init;
	d_socket_init.setPort(CLIENT_PORT);
	d_socket_init.setHints(1);
	if(d_socket_init.bind() != 0){
		return 2;
	}
	if(d_socket_init.listen() != 0){
		return 3;
	}

	printf("Successfully started on PORT: %d\n", d_socket_init.getPort());
	
	//sending this new_port to the server
	if(sendControl(getStrFromInt(CLIENT_PORT)) == -1){
		return 4;
	}

	d_socket = d_socket_init.accept();
	if(d_socket.getSocket() == -1){
		return 5;
	}
	
	printf("Successfully connected to server(data port)\n");
	printf("Type 'help' for more info\n\n");

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rsi, -0x270(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x3110
movq -0x268(%rbp), %rdi
movq -0x260(%rbp), %rsi
callq 0x3770
jmp 0x4acd
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0x268(%rbp), %rdi
movl -0x1c(%rbp), %eax
movzwl %ax, %esi
callq 0x37a0
movq -0x268(%rbp), %rdi
xorl %esi, %esi
callq 0x37c0
movq -0x268(%rbp), %rdi
callq 0x3960
cmpl $0x0, %eax
je 0x4b2d
movl $0x1, -0x4(%rbp)
jmp 0x4d58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3160
jmp 0x4d64
movq 0x649c(%rip), %rdi      # 0xafd0
leaq 0x35d0(%rip), %rsi      # 0x810b
callq 0x3250
movq -0x270(%rbp), %rsi
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
movq 0x645f(%rip), %rsi      # 0xafb8
callq 0x3280
leaq -0x140(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x3660
movq -0x280(%rbp), %rdi
movl $0x4e20, %esi           # imm = 0x4E20
callq 0x37a0
movq -0x280(%rbp), %rdi
movl $0x1, %esi
callq 0x37c0
movq -0x280(%rbp), %rdi
callq 0x3c00
movl %eax, -0x274(%rbp)
jmp 0x4ba7
movl -0x274(%rbp), %eax
cmpl $0x0, %eax
je 0x4bd9
movl $0x2, -0x4(%rbp)
movl $0x1, -0x144(%rbp)
jmp 0x4d3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4d4a
leaq -0x140(%rbp), %rdi
callq 0x3ea0
movl %eax, -0x284(%rbp)
jmp 0x4bed
movl -0x284(%rbp), %eax
cmpl $0x0, %eax
je 0x4c0e
movl $0x3, -0x4(%rbp)
movl $0x1, -0x144(%rbp)
jmp 0x4d3c
leaq -0x140(%rbp), %rdi
callq 0x3710
movzwl %ax, %esi
leaq 0x351b(%rip), %rdi      # 0x813f
xorl %eax, %eax
callq 0x3050
jmp 0x4c2d
leaq -0x168(%rbp), %rdi
movl $0x4e20, %esi           # imm = 0x4E20
callq 0x4d70
jmp 0x4c40
movq -0x268(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x4210
movl %eax, -0x288(%rbp)
jmp 0x4c5b
leaq -0x168(%rbp), %rdi
callq 0x3160
movl -0x288(%rbp), %eax
cmpl $-0x1, %eax
jne 0x4ca5
movl $0x4, -0x4(%rbp)
movl $0x1, -0x144(%rbp)
jmp 0x4d3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3160
jmp 0x4d4a
leaq -0x258(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x3ef0
jmp 0x4cba
movq -0x268(%rbp), %rdi
addq $0xf0, %rdi
leaq -0x258(%rbp), %rsi
callq 0x73c0
leaq -0x258(%rbp), %rdi
callq 0x7420
movq -0x268(%rbp), %rdi
addq $0xf0, %rdi
callq 0x3760
cmpl $-0x1, %eax
jne 0x4d0b
movl $0x5, -0x4(%rbp)
movl $0x1, -0x144(%rbp)
jmp 0x4d3c
leaq 0x344f(%rip), %rdi      # 0x8161
xorl %eax, %eax
callq 0x3050
jmp 0x4d1b
leaq 0x346c(%rip), %rdi      # 0x818e
xorl %eax, %eax
callq 0x3050
jmp 0x4d2b
movl $0x0, -0x4(%rbp)
movl $0x1, -0x144(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x7420
jmp 0x4d58
leaq -0x140(%rbp), %rdi
callq 0x7420
jmp 0x4d64
movl -0x4(%rbp), %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3440
nopl (%rax)