<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def forward(self, device_port, local_port=None):
        '''adb port forward. return local_port'''
        if local_port is None:
            for s, lp, rp in self.forward_list():
                if s == self.device_serial() and rp == 'tcp:%d' % device_port:
                    return int(lp[4:])
            return self.forward(device_port, next_local_port(self.server_host))
        else:
            self.cmd("forward", "tcp:%d" % local_port, "tcp:%d" % device_port).wait()
            return local_port