<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def reserve_tcp_port(self, port, project, port_range_start=None, port_range_end=None):
 """ Reserve a specific TCP port number. If not available replace it by another. :param port: TCP port number :param project: Project instance :param port_range_start: Port range to use :param port_range_end: Port range to use :returns: The TCP port """


        # use the default range is not specific one is given
        if port_range_start is None and port_range_end is None:
            port_range_start = self._console_port_range[0]
            port_range_end = self._console_port_range[1]

        if port in self._used_tcp_ports:
            old_port = port
            port = self.get_free_tcp_port(project, port_range_start=port_range_start, port_range_end=port_range_end)
            msg = "TCP port {} already in use on host {}. Port has been replaced by {}".format(old_port, self._console_host, port)
            log.debug(msg)
            #project.emit("log.warning", {"message": msg})
            return port
        if port < port_range_start or port > port_range_end:
            old_port = port
            port = self.get_free_tcp_port(project, port_range_start=port_range_start, port_range_end=port_range_end)
            msg = "TCP port {} is outside the range {}-{} on host {}. Port has been replaced by {}".format(old_port, port_range_start, port_range_end, self._console_host, port)
            log.debug(msg)
            #project.emit("log.warning", {"message": msg})
            return port
        try:
            PortManager._check_port(self._console_host, port, "TCP")
        except OSError:
            old_port = port
            port = self.get_free_tcp_port(project, port_range_start=port_range_start, port_range_end=port_range_end)
            msg = "TCP port {} already in use on host {}. Port has been replaced by {}".format(old_port, self._console_host, port)
            log.debug(msg)
            #project.emit("log.warning", {"message": msg})
            return port

        self._used_tcp_ports.add(port)
        project.record_tcp_port(port)
        log.debug("TCP port {} has been reserved".format(port))
        return port